java面试-数据结构和算法

1.排序

1.1 冒泡排序


 
  1. package sort;
  2. /**
  3. * Created by david on 2018/8/16
  4. * 冒泡排序
  5. */
  6. public class BubbleSort {
  7. private static int[] bubbleSort( int[] a) {
  8. int len = a.length;
  9. for ( int i = 1; i < len - 1; i++) {
  10. for ( int j = 1; j < len - 1-i; j++) {
  11. if (a[j + 1] < a[j]) {
  12. swap(a, j + 1, j);
  13. }
  14. }
  15. }
  16. return a;
  17. }
  18. //交换方法
  19. private static void swap(int[] a, int i, int j) {
  20. int tmp = a[i];
  21. a[i] = a[j];
  22. a[j] = tmp;
  23. }
  24. //测试
  25. public static void main(String[] args) {
  26. int[] a = { 1, 4, 6, 8, 99, 9, 2, 99};
  27. int[] sort = bubbleSort(a);
  28. for ( int s : sort) {
  29. System.out.print(s + " ");
  30. }
  31. }
  32. }

1.2快速排序


 
  1. package sort;
  2. /**
  3. * Created by david on 2018/8/16
  4. * 快速排序
  5. * 不稳定,时间复杂度 最理想 O(nlogn) 最差时间O(n^2)
  6. */
  7. public class QuickSort {
  8. private static int[] quickSort( int[] a, int low, int high) {
  9. //中心点
  10. int mid = 0;
  11. if (low < high) {
  12. mid = partition(a, low, high);
  13. quickSort(a, low, mid - 1);
  14. quickSort(a, mid + 1, high);
  15. }
  16. return a;
  17. }
  18. private static int partition(int[] a, int low, int high) {
  19. int b = a[low];
  20. while (low < high) {
  21. while (low < high && a[high] >= b) {
  22. high--;
  23. }
  24. a[low] = a[high];
  25. while (low < high && a[low] <= b) {
  26. low++;
  27. }
  28. a[high] = a[low];
  29. }
  30. a[low] = b;
  31. return low;
  32. }
  33. //测试
  34. public static void main(String[] args) {
  35. int[] a = { 1, 14, 6, 8, 99, 9, 2, 99};
  36. int[] sort = quickSort(a, 0, 7);
  37. for ( int s : sort) {
  38. System.out.print(s + " ");
  39. }
  40. }
  41. }

1.3 二分查找


 
  1. package sort;
  2. /**
  3. * Created by david on 2018/8/16
  4. * 查找前的数据必须是已经排好序的, 然后得到数组的开始位置start和结束位置end,
  5. * 取中间位置mid的数据a[mid]跟待查找数据key进行比较, 若 a[mid] > key, 则取end = mid - 1;
  6. * 若 a[mid] < key, 则取start = mid + 1; 若 a[mid] = key 则直接返回当前mid为查找到的位置.
  7. * 依次遍历直到找到数据或者最终没有该条数据
  8. */
  9. public class BinarySearch {
  10. public static int binarySearch(int[] a, int key) {
  11. int start = 0;
  12. int end = a.length - 1;
  13. int mid = - 1;
  14. while (start <= end) {
  15. mid = (start + end) / 2;
  16. if (a[mid] == key) {
  17. return mid;
  18. } else if (a[mid] > key) {
  19. end = mid - 1;
  20. } else if (a[mid] < key) {
  21. start = mid + 1;
  22. }
  23. }
  24. return - 1;
  25. }
  26. // 测试
  27. public static void main(String[] args) {
  28. int[] a = { 1, 4, 6, 8, 99};
  29. int i = binarySearch(a, 99);
  30. System.out.println(i);
  31. }
  32. }

1.3 String与Array转换


 
  1. import java.util.Arrays;
  2. /**
  3. * Created by david on 2018/8/16
  4. * String/Array转换
  5. */
  6. public class Convert {
  7. public static void main(String[] args) {
  8. String str = "we are family";
  9. //String转成Array
  10. char[] chars = str.toCharArray();
  11. //排序
  12. Arrays.sort(chars);
  13. //转成String
  14. String s = Arrays.toString(chars);
  15. //根据index获得char
  16. char c = str.charAt( 7);
  17. //长度
  18. str.length();
  19. int length = chars.length;
  20. //子串
  21. String substring1 = str.substring( 1, 4);
  22. String substring2 = str.substring( 3);
  23. //int转string
  24. Integer integer = Integer.valueOf( "3");
  25. //string转int
  26. String value = String.valueOf( 3);
  27. }
  28. }

1.4 单链表反转


 
  1. public class Node {
  2. //为了方便,这两个变量都使用public,而不用private就不需要编写get、set方法了。
  3. //存放数据的变量,简单点,直接为int型
  4. public int data;
  5. //存放结点的变量,默认为null
  6. public Node next;
  7. //构造方法,在构造时就能够给data赋值
  8. public Node(int data){
  9. this.data = data;
  10. }
  11. }

 
  1. package link;
  2. /**
  3. * Created by david on 2018/8/16
  4. * 单链表反转
  5. */
  6. public class NodeRe {
  7. public static void main(String[] args) {
  8. Node head = new Node( 0);
  9. Node node1 = new Node( 1);
  10. Node node2 = new Node( 2);
  11. Node node3 = new Node( 3);
  12. head.setNext(node1);
  13. node1.setNext(node2);
  14. node2.setNext(node3);
  15. // 调用反转方法
  16. head = reverse(head);
  17. // 打印反转后的结果
  18. while ( null != head) {
  19. System.out.print(head.getData() + " ");
  20. head = head.getNext();
  21. }
  22. }
  23. public static Node reverse(Node head){
  24. // head看作是前一结点,head.getNext()是当前结点,
  25. // reHead是反转后新链表的头结点
  26. if(head == null || head.getNext() == null){
  27. // 若为空链或者当前结点在尾结点,则直接还回
  28. return head;
  29. }
  30. Node reHead = reverse(head.getNext());
  31. // 将当前结点的指针域指向前一结点
  32. head.getNext().setNext(head);
  33. // 前一结点的指针域令为null;
  34. head.setNext( null);
  35. // 反转后新链表的头结点
  36. return reHead;
  37. }
  38. }

1.5 双向链表反转

双向链表反转

1.6 多线程

多线程

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  •                     <li class="tool-item tool-active is-like eye-protector-processed" style="transition: background-color 0.3s ease 0s; background-color: rgb(193, 230, 198);"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#csdnc-thumbsup"></use>
                        </svg><span class="name">点赞</span>
                        <span class="count"></span>
                        </a></li>
                        <li class="tool-item tool-active is-collection eye-protector-processed" style="transition: background-color 0.3s ease 0s; background-color: rgb(193, 230, 198);"><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-Collection-G"></use>
                        </svg><span class="name">收藏</span></a></li>
                        <li class="tool-item tool-active is-share eye-protector-processed" style="transition: background-color 0.3s ease 0s; background-color: rgb(193, 230, 198);"><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-fenxiang"></use>
                        </svg>分享</a></li>
                        <!--打赏开始-->
                                                <!--打赏结束-->
                                                <li class="tool-item tool-more">
                            <a>
                            <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                            </a>
                            <ul class="more-box eye-protector-processed" style="transition: background-color 0.3s ease 0s; border-color: rgba(0, 0, 0, 0.35); background-color: rgb(193, 230, 198);">
                                <li class="item"><a class="article-report">文章举报</a></li>
                            </ul>
                        </li>
                                            </ul>
                </div>
                            </div>
            <div class="person-messagebox eye-protector-processed" style="border-top-color: rgba(0, 0, 0, 0.35);">
                <div class="left-message"><a href="https://blog.csdn.net/a1032818891">
                    <img src="https://profile.csdnimg.cn/5/2/5/3_a1032818891" class="avatar_pic" username="a1032818891">
                                            <img src="https://g.csdnimg.cn/static/user-reg-year/1x/7.png" class="user-years">
                                    </a></div>
                <div class="middle-message">
                                        <div class="title"><span class="tit"><a href="https://blog.csdn.net/a1032818891" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">David在学习</a></span>
                                            </div>
                    <div class="text"><span>发布了35 篇原创文章</span> · <span>获赞 8</span> · <span>访问量 3万+</span></div>
                </div>
                                <div class="right-message">
                                            <a href="https://im.csdn.net/im/main.html?userName=a1032818891" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter eye-protector-processed" style="transition: background-color 0.3s ease 0s; background-color: rgb(193, 230, 198);">私信
                        </a>
                                                            <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
                    </div>
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值