Pass-through BAT-sort 1 (bubble sort, selection sort, insertion sort, merge sort, quick sort, heap sort, hill sort)

  • Sorting algorithm with O(N^2) time complexity

  • Bubble Sort:

import java.util.*;

public class BubbleSort {
    public int[] bubbleSort(int[] A, int n) {
        // write code here
        int temp;
        for(int i = n - 1; i >= 0; i--) {
            for(int j = 0; j < i; j++) {
                if(A[j] > A[j + 1]) {
                    temp = A[j];
                    A[j] = A[j + 1];
                    A[j + 1] = temp;
                }
            }
        }
        return A;
    }
}
  • selection sort
import java.util.*;

public class SelectionSort {
    public int[] selectionSort(int[] A, int n) {
        // write code here
        int temp;
        for(int i = 0; i < n; i++) {
            for(int j = i + 1; j < n; j++) {
                if(A[j] < A[i]) {
                    temp = A[i];
                    A[i] = A[j];
                    A[j] = temp;
                }
            }
        }
        return A;
    }
}
  • Insertion sort
import java.util.*;

public class InsertionSort {
    public int[] insertionSort(int[] A, int n) {
        // write code here
        int temp;
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < i; j++) {
                if(A[i] < A[j]) {
                    temp = A[i];
                    A[i] = A[j];
                    A[j] = temp;
                }
            }
        }
        return A;
    }
}
  • Sorting algorithm with O(N*logN) time complexity

  • merge sort

import java.util.*;

public class MergeSort {
    public int[] mergeSort(int[] A, int n) {
        // write code here
        if(A == null || n < 2) {
            return A;
        }
        process(A, 0, n - 1);
        return A;
    }
    public static void process(int[] A, int left, int right) {
        if(left == right) {
            return;
        }
        int mid = (left + right) / 2;
        process(A, left, mid);
        process(A, mid + 1, right);
        merge(A, left, mid, right);
    }
    public static void merge(int[] A, int left, int mid, int right) {
        int[] help = new int[right - left + 1];
        int l = left;
        int r = mid + 1;
        int index = 0;
        while (l <= mid && r <= right) {
            if (A[l] <= A[r]) {
                help[index++] = A[l++];
            } else {
                help[index++] = A[r++];
            }
        }
        while (l <= mid) {
            help[index++] = A[l++];
        }
        while (r <= right) {
            help[index++] = A[r++];
        }
        for (int i = 0; i < help.length; i++) {
            A[left + i] = help[i];
        }
    }
}
  • quicksort
import java.util.*;

public class QuickSort {
    public int[] quickSort(int[] A, int n) {
        // write code here
        if(A == null || n < 2) {
            return A;
        }
        process(A, 0, n - 1);
        return A;
    }
    public static void process(int[] A, int left, int right) {
        if(left < right) {
            int random = left + (int)(Math.random() * (right - left + 1));
            swap(A, random, right);
            int mid = partition(A, left, right);
            process(A, left, mid - 1);
            process(A, mid + 1, right);
        }
    }
    public static int partition(int[] A, int left, int right) {
        int pivot = left - 1;
        int index = left;
        while (index <= right) {
            if (A[index] <= A[right]) {
                swap(A, ++pivot, index);
            }
            index++;
        }
        return pivot;
    }

    public static void swap(int[] A, int index1, int index2) {
        int tmp = A[index1];
        A[index1] = A[index2];
        A[index2] = tmp;
    }
}
  • heap sort
import java.util.*;

public class HeapSort {
    public int[] heapSort(int[] A, int n) {
        // write code here
        for(int i=n/2; i>=0; i--){
            heapAdjust(A,i,n);
        }

        for(int i=n-1;i>0;i--){
            swap(A,0,i);
            heapAdjust(A,0,i);
        }
        return A;
    }

    void heapAdjust(int[] A,int index,int length){
        int childLeft;
        int temp = A[index];
        for( ;index*2+1 < length;index = childLeft){
            childLeft = index*2+1;
            if(childLeft !=length-1 && A[childLeft] < A[childLeft+1]){
                 childLeft++;
            }
            if(temp > A[childLeft]){
                break;

            }                
            else {
                A[index] = A[childLeft];
                index = childLeft;
            }           
        }
         A[index] = temp;

    }

    static void  swap(int[] A,int m,int n){
        int temp = A[m];
        A[m] = A[n];
        A[n] = temp;
    }
}
  • Hill sort
import java.util.*;

public class ShellSort {
    public int[] shellSort(int[] A, int n) {
        if(A == null || n < 2) {
            return A;
        }
        int feet = n / 2;
        int index = 0;
        while(feet > 0){
            for(int i = feet; i < n; i++) {
                index = i;
                while(index >= feet) {
                    if(A[index - feet] > A[index]) {
                        swap(A, index - feet, index);
                        index -= feet;
                    } else {
                        break;
                    }
                }
            }
            feet /= 2;
        }
        return A;
    }
    public static void swap(int[] A, int index1, int index2) {
        int temp = A[index1];
        A[index1] = A[index2];
        A[index2] = temp;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325817835&siteId=291194637