sorting algorithms implemented in Java

binary insertion sort

//binary insertion sort

import java.util.Scanner;

public class Main{

   public static void main(String[] args){
       Scanner sc = new Scanner(System.in);
       String str = sc.nextLine();
       String[] strArr = str.split(",");
       int[] nums = new int[strArr.length];
       for(int i = 0;i<nums.length;i++){
           nums[i] = Integer.valueOf(strArr[i]);
       }
       
       binaryInsertionSort(nums);

       for(int num : nums){
          System.out.printf("%d ", num);
       }

   }

   public static void binaryInsertionSort(int[] arr){
       int len = arr.length;
       int tempVal, left, right, mid;
       for(int i=1;i<len;i++){
           if(arr[i]<arr[i-1]){
               tempVal = arr[i];
               left = 0;
               right = i-1;
               while(left<=right){
                   mid = (left+right)/2;
                   if(tempVal < arr[mid]){
                       right = mid -1;
                   }else{
                       left = mid + 1;
                   }
               }
               for(int j=i;j>left;j--){
                   arr[j] = arr[j-1];
               }
               arr[left] = tempVal;
           }
       }
   }

}

insertion sort

    //insertion sort
    public static void insertionSort(int[] arr){
        int len = arr.length;
        int right, tempVal;
        for(int i=1;i<len;i++){
           if(arr[i]<arr[i-1]){
               tempVal = arr[i];
               right = i-1;
               while(right>=0){
                   if(tempVal<arr[right]){
                     right--;
                   }else{
                       break;
                   }
               }
               if(right<0) right =0;
               for(int j=i;j>right;j--){
                   arr[j] = arr[j-1];
               }
               arr[right] = tempVal;
           }
        }
    }

bubble sort

    //bubble sort
    public static void bubbleSort(int[] arr){
        int len = arr.length;
        for(int i=1;i<len;i++){
            for(int j=0;j<len-i;j++){
                if(arr[j]>arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }

quick sort

    //quick sort
    public static void quickSort(int[] arr, int left, int right){
        if(left >= right)return;
        int i=left, j=right;
        int pilot = arr[left];
        while(i<j){
            while(i<j && arr[j]>=pilot)j--;
            if(i<j){arr[i] = arr[j]; i++;}
            

            while(i<j && arr[i]<pilot)i++;
            if(i<j)arr[j--] = arr[i];
        }
        arr[i]=pilot;
        quickSort(arr, left, i-1);
        quickSort(arr, i+1, right);
    } 

merge sort

    //merge sort
    public static int[] sort(int[] sourceArray){
        int len = sourceArray.length;
        if(len == 1) return sourceArray;
        int middle = len/2;
        int[] leftPart = Arrays.copyOfRange(sourceArray, 0, middle);
        int[] rightPart = Arrays.copyOfRange(sourceArray, middle, len);
        return merge(sort(leftPart), sort(rightPart));
    }

    public static int[] merge(int[] left, int[] right){
        int[] result = new int[left.length+right.length];
        int cnt = 0;
        while(left.length>0 && right.length>0){
            if(left[0]<right[0]){
                result[cnt++] = left[0];
                left = Arrays.copyOfRange(left, 1, left.length);
            }else{
                result[cnt++] = right[0];
                right = Arrays.copyOfRange(right, 1, right.length);
            }
        }
        if(left.length>0){
            for(int i : left){
                result[cnt++] = i;
            }
        }

        if(right.length>0){
            for(int j : right){
                result[cnt++] = j;
            }
        }
        return result;
    }

heap sort

 //heap sort(in-place algorithm)
    protected static void heapSort(int[] numList){
        int len = numList.length;
        if(len <= 1) return;
        buildUpHeap(numList);
        for(int i= len-1;i>0;i--){
            if(numList[i]<numList[0]){
                swap(numList, 0, i);
                adjustHeap(numList, i-1);
            }
        }
    }

    static void swap(int[] l, int index0, int index1){
        int temp = l[index0];
        l[index0] = l[index1];
        l[index1] = temp;
    }

    static void buildUpHeap(int[] list){
        int update;
        do{
          update=0;
          for(int i =0;i*2+1<list.length;i++){
              if(i*2+2<list.length){
                  if(list[i*2+2]>list[i] && list[i*2+2]>list[i*2+1]){
                      swap(list, i*2+2, i);
                      update++;
                  }else if(list[i*2+1]>list[i*2+2] && list[i*2+1]>list[i]){
                      swap(list, i*2+1, i);
                      update++;
                  }
              }else{
                  if(list[i*2+1]>list[i]){
                      swap(list, i*2+1, i);
                      update++;
                  }
              }
          }
        }while(update>0);
    }

    protected static void adjustHeap(int[] list, int border){
        int position = 0;
        while(position*2+1<=border){
            if(position*2+2<=border){
                if(list[position*2+2] > list[position] && list[position*2+2]>list[position*2+1]){
                    swap(list, position*2+2, position);
                    position = position*2+2;
                }else if(list[position*2+1]>list[position] && list[position*2+1]>list[position*2+2]){
                    swap(list, position*2+1, position);
                    position = position*2+1;
                }else break;
            }else{
                if(list[position*2+1]>list[position]){
                    swap(list, position*2+1, position);
                    position = position*2 + 1;
                }else break;
            }
        }
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值