几种比较常见的算法排序

时间:2023-02-17 10:12:40

归并排序

    在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007。

 1 public class Solution {
 2     int cnt;
 3     public int InversePairs(int [] array) {
 4         cnt = 0;
 5         if (array != null)
 6             mergeSortUp2Down(array, 0, array.length - 1);
 7         return cnt;
 8     }
 9  public void mergeSortUp2Down(int[] a, int start, int end) {
10         if (start >= end)
11             return;
12         int mid = (start + end) >> 1;
13  
14         mergeSortUp2Down(a, start, mid);
15         mergeSortUp2Down(a, mid + 1, end);
16  
17         merge(a, start, mid, end);
18     }
19     public void merge(int[] a, int start, int mid, int end) {
20         int[] tmp = new int[end - start + 1];
21  
22         int i = start, j = mid + 1, k = 0;
23         while (i <= mid && j <= end) {
24             if (a[i] <= a[j])
25                 tmp[k++] = a[i++];
26             else {
27                 tmp[k++] = a[j++];
28                 cnt =(cnt+ mid - i + 1)%100000000729             }
30         }
31         while (i <= mid)
32             tmp[k++] = a[i++];
33         while (j <= end)
34             tmp[k++] = a[j++];
35         for (k = 0; k < tmp.length; k++)
36             a[start + k] = tmp[k];
37     }
38 }

 针对上面同一问题

冒泡排序

 1  public class Solution {
 2       public int InversePairs(int [] number) {
 3         int count=0; 5         
4 for(int i1=0;i1<number.length;i1++){ 6 for(int j=i1;j<number.length;j++){ 7 if(number[i1]>number[j]){ 8 count++; 9 } 10 } 11 } 12 return count; 13 } 14 }

插入排序

 1  public class Solution {
 2       public int InversePairs(int [] number) {
 3           int count1 = 0;
 4         for(int i1=1;i1<number.length;i1++){
 5             int temp = number[i1];
 6             int j=i1;
 7             while(j>0&&temp<number[j-1]){
 8                 number[j] = number[j-1];
 9                 number[j-1] = temp;
10                 count1++;
11                 j--;
12             }
13         }
14           return count1;
15     }
16 }

回溯法

   输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba

 1 import java.util.ArrayList;
 2 import java.util.TreeSet;
 3 public class Solution {
 4     public ArrayList<String> Permutation(String str) {
 5        ArrayList<String> list = new ArrayList<String>();
 6         if(str == null||str.length()==0)
 7             return list;
 8        TreeSet<String> set = new TreeSet<String>();
 9         fun(set,str.toCharArray(),0);
10         list.addAll(set);
11         return list;
12     }
13     public void fun(TreeSet set,char[] str,int k){
14         if(str.length == k){
15             set.add(new String(str));
16             return;
17         }
18         for(int i=k;i<str.length;i++){
19             swap(str,i,k);
20                 fun(set,str,k+1);
21             swap(str,i,k);
22         }
23     }
24     public void swap(char[] str,int i,int j){
25             if(i!=j){
26                 char t = str[i];
27                 str[i] = str[j];
28                 str[j] = t;
29             }
30         }
31 }

插入排序

    输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。

 1 public class Solution {
 2     public void reOrderArray(int [] array) {
 3         int temp;
 4         int index;
 5         for(int i=0;i<array.length;i++){
 6             if((array[i]&1)==1){
 7                 temp = array[i];
 8                 index = i-1;
 9                 while(index>=0 && array[index]%2==0){
10                     array[index+1]=array[index];
11                     index--;
12                 }
13                 array[index+1]=temp;
14             }
15         }
16     }
17 }

递归排序

   输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

 1 /**
 2 public class TreeNode {
 3     int val = 0;
 4     TreeNode left = null;
 5     TreeNode right = null;
 6 
 7     public TreeNode(int val) {
 8         this.val = val;
 9 
10     }
12 }
13 */
14 public class Solution {
15     public boolean HasSubtree(TreeNode root1,TreeNode root2) {
16         boolean result=false;
17         if(root2 == null || root1 == null)
18             return false;
19         if(root1.val == root2.val){
20             result = isSubtree(root1,root2);
21         }
22         if(!result){
23             result = HasSubtree(root1.left,root2);
24         }
25         if(!result){
26             result = HasSubtree(root1.right,root2);
27         }
28         return result;
29     }
30      public static boolean isSubtree(TreeNode root1,TreeNode root2){
31         if(root2 == null)
32             return true;
33          if(root1 == null)
34              return false;
35          if(root1.val != root2.val)
36              return false;
37          return isSubtree(root1.left,root2.left)&&isSubtree(root1.right,root2.right);
38     }
39 }