Java 排序算法、字符串反转、链表反转、数组去重

177 阅读2分钟
  1. 字符串反转
	private static void reverse1(String str){
            String res="";
            int length = str.length();
            for(int i=0; i<length; i++){
                    res = str.charAt(i) + res;
            }
            System.out.println("reverse1===>" + res);
	}


	private static void reverse2(String str){
            String res="";
            int length = str.length();
            for(int i=length-1; i>=0; i--){
                    res += str.charAt(i);
            }
            System.out.println("reverse2===>" + res);
	}

  1. 链表反转
public class Test{
    public static void main(String[] args){
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);  
        Node node4 = new Node(4);
        
        node1.setNext(node2);
        node2.setNext(node3);
        node3.setNext(node4);
        
        Node head = node1;
        while(head != null){
            System.out.print(head.getData() + " ");
            head = head.getNext();
        }
        
        System.out.println("\n " + "=========反转后=========" + "\n");
		
        head = reverse(node1);
        while(head != null){
            System.out.print(head.getData() + " ");
            head = head.getNext();
        }
    }
    
    private static Node reverse(Node head){
        if(head == null || head.getNext() ==null){
            return head;
        }
        
        Node reNode = reverse(head.getNext());
        head.getNext().setNext(head);
        head.setNext(null);
	return reNode;
    }
}

class Node{
    private int data;
    private Node next;
    
    Node(int data){
        this.data = data;
    }
    
    public int getData(){
        return data;
    }
    
    public void setData(int data){
        this.data = data;
    }
    
    public Node getNext(){
        return next;
    }
    
    public void setNext(Node node){
        this.next = node;
    }
}
  1. 数组去重
private static void removeMore1(String[] strings){
    Set<String> set = new HashSet();
    set.add(strings[0]);
    for(int i=1; i<strings.length; i++){
       set.add(strings[i]);
    }
    String[] res = (String[])set.toArray(new String[set.size()]);
    System.out.println(Arrays.toString(res) + "\n");
}

private static void removeMore2(String[] strings){
    List<String> list = new ArrayList();
    list.add(strings[0]);
    for(int i=1; i<strings.length; i++){
            if(!list.contains(strings[i])){
             list.add(strings[i]);
            }
    }
    String[] res = (String[])list.toArray(new String[list.size()]);
    System.out.println(Arrays.toString(res) + "\n");
}
  1. 常见排序
/**
* 冒泡排序
* 遍历所有元素,每个元素与之后元素进行对比,数值较大则交换位置,单次比较后可以找到最大值
* 
**/
private static void buddleSort(int[] a){
    for(int i=0; i<a.length; i++){
        for(int j=0; j<a.length-i-1; j++){
            if(a[j]>a[j+1]){
                int temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
            }
        }
    }
    System.out.println(Arrays.toString(a) + "\n");
}


/**
* 选择排序
* 找出数组中最大元素的下标,与数组的最后一个元素进行交互,类似冒泡排序,但只进行一次交换
* 
**/
private static void selectSort(int[] a){
    for(int i=0; i<a.length;i++){
        int max=0;
        for(int j=0; j<a.length-i;j++){
            if(a[max]<=a[j]){
                max = j;
            }
        }
        int temp=a[a.length-1-i];
        a[a.length-1-i] = a[max];
        a[max] = temp;
    }
    System.out.println(Arrays.toString(a) + "\n");
}

/**
* 快速排序
* 以第一个元素作为基准,先从右侧开始查找如果小于右侧元素则向左移位,如果大于右侧元素进行交换
* 再从左侧开始查找,如果大于左侧元素则向右移位,若小于左侧元素进行交换
**/
private static void quickSort(int[] a, int left, int right){
    if(left>=right){
        return;
    }
    int low = left;
    int high = right;
     int temp = a[low];
    while(low<high){
        while(low<high && temp<=a[high]){
            high--;
        }
        a[low] = a[high];
         while(low<high && temp>=a[low]){
            low++;
        }
        a[high] = a[low];
    }
    a[low] = temp;
    quickSort(a,left, low-1);
    quickSort(a,low+1,right);
    System.out.println(Arrays.toString(a) + "\n");
}


/**
* 插入排序
* 将数组中各个元素做为基准,将其与之前的元素进行比较,若比基准大则进行移位操作
* 若比基准元素小,该元素之后的位置即为插入位置
**/
private static void insertSort(int[] a){
    for(int i=1; i<a.length; i++){
        int temp = a[i];
        int j=i-1;
        for(; j>=0; j--){
            if(temp<a[j]){
                a[j+1] = a[j];
            }else{
                break;
            }
    }
        a[j+1] = temp;
    }
    System.out.println(Arrays.toString(a) + "\n");
}


/**
* 二分法插入排序
* 将数组中各个元素做为基准,将其与之前已经排序过的数组进行进行比较,采用二分法从中间元素开始进行比较
* 进行移位操作,找到插入位置
**/
private static void binaryInsertSort(int[] a){
    for(int i=1; i<a.length;i++){
        int temp = a[i];
        int left=0, right=i-1;
        int mid;
        while(left<=right){
             mid=(left+right)/2;
            if(temp<=a[mid]){
                right=mid-1;
            }else{
                left=mid+1;
            }
        }
       
        for(int j=i-1; j>=left; j--){
            a[j+1]=a[j];
        }
        a[left]= temp;
     }
    System.out.println(Arrays.toString(a) + "\n");
}

/**
* 希尔排序
* 根据数组列表长度确定比较跨度gap, 跨度两个值进行比较,进行位置互换,直到跨度为1
**/
private static void shellSort(int[] a){
    int gap=a.length;
    while(true){
        gap=gap/2;
        for(int i=0;i<=gap;i++){
            for(int j=0;(j+gap)<a.length;j+=gap){
                if(a[j]>a[j+gap]){
                    int temp=a[j+gap];
                    a[j+gap]=a[j];
                    a[j]=temp;
                }
            }
        }
        if(gap ==1){
            break;
        }
    }
    System.out.println(Arrays.toString(a) + "\n");
}
  

参考文献: Java 9种排序算法详解和示例汇总 - 掘金 (juejin.cn)