Exchange of Sorting Algorithms (Bubble and Fast Row)

Keywords: Java less

The sorting algorithm can be divided into internal sorting and external sorting according to whether external memory needs to be accessed or not.

  • Internal sorting refers to the sorting process in which the sorting column is stored in memory completely, and it is suitable for the less large element sequence.
  • External sorting refers to the sorting of large files, that is to say, the records to be sorted are stored in external memory, and the files to be sorted can not be loaded into memory at one time. It is necessary to exchange data between memory and external memory for many times to achieve the purpose of sorting the whole file.

I. Internal Sorting

Using memory, it can be divided into insertion (direct insertion sort Hill sort), selection (simple selection sort heap sort), exchange (bubble sort quick sort), merge, cardinality.

5. Bubble Sorting

Low efficiency and simple implementation

  • Basic idea: Compare the adjacent elements in pairs, and exchange them in opposite order. Each sorting will place the smallest or largest elements at the end of the array to be sorted, and ultimately achieve complete order.

Columns to be sorted: 3, 1, 4, 2
First ranking results: 1, 3, 2, 4
Second ranking results: 1, 2, 3, 4
The third ranking result: 1, 2, 3, 4

The final ranking results are: 1, 2, 3, 4

  • Code Implementation (Java)
public class BubbleSort {
	public static void bubbleSort(int[] arr){
        for (int i=0;i<arr.length-1; i++){
            //Set a flag as a flag that no data exchange occurs. If the loop is not swapped, the sorting is complete                
            boolean flag = true;
            //There are already i elements in order.
            for (int j=1;j<arr.length-i; j++){
                if(arr[j-1] > arr[j]){
                    swap(arr,j-1,j);
                    flag = false;
                }
            }
            System.out.println("The first"+(i+1)+"The result of trip sorting is as follows:");
            display(arr);
            if(flag){
                break;
            }
        }
    }
	public static void swap(int[] arr, int a,int b) {
		if(arr[a]>arr[b]) {
			arr[a]=arr[a]+arr[b];
			arr[b]=arr[a]-arr[b];
			arr[a]=arr[a]-arr[b];
		}
	}
	public static void display(int[] arr) {
		for(int i=0;i<arr.length;i++) {
			System.out.print(arr[i]+"  ");
		}
		System.out.println();
	}
	public static void main(String[] args) {
		int arr[]= {9,3,1,4,2,7,8,6,5};
		bubbleSort(arr);
		System.out.println("The final ranking results are as follows:");
		display(arr);
	}
}
  • algorithm analysis

The time complexity of bubble sort is O(n^2). Worst case, reverse order O (n*(n-1)/2); best case, order O(n-1).

6. Quick Sorting

Divide and conquer algorithm

  • Basic ideas:
    Choose a baseline element, usually the first or last one.
    Through one-step sorting, the elements to be arranged are divided into two parts, one part is smaller than the base element, the other part is larger than the base element.
    At this point, the reference element is in the correct position of the final sequence. The two parts are sorted by the same method until the whole sequence is ordered.
  • Code implementation (Java):
public class QuickSort {
	public static void quickSort(int[] arr) {
		if(arr.length<0 || arr==null) {
			return;
		}
	    quickSort(arr,0,arr.length-1);
	}
	public static void quickSort(int[] arr, int low, int high) {
		if(low>=high) {
			return;
		}else {
			int flag=arr[low];
			int partition=partition(arr,low,high,flag);
			display(arr);
			quickSort(arr,low,partition-1);
			quickSort(arr,partition+1,high);
		}
	}
	public static int partition(int[] arr,int low,int high,int flag) {
		while(low<high) {
			while(low<high && arr[high]>=flag) {
				high--;
			}
			swap(arr,low,high);
			while(low<high && arr[low]<=flag) {
				low++;
			}
			swap(arr,low,high);
		}
		arr[low]=flag;
		return low;
	}
	public static void swap(int[] arr, int a,int b) {
			arr[a]=arr[a]+arr[b];
			arr[b]=arr[a]-arr[b];
			arr[a]=arr[a]-arr[b];
	}
	public static void display(int[] arr) {
		for(int i=0;i<arr.length;i++) {
			System.out.print(arr[i]+"  ");
		}
		System.out.println();
	}
	public static void main(String[] args) {
		int[] arr= {57,68,59,52,72,28,96,33,24,19};
		quickSort(arr);
		System.out.println("Final results:");
		display(arr);
	}
}
  • algorithm analysis

The time complexity is O (n*logn). When the data is arranged from small to large or from large to small, the efficiency of quick sorting is the lowest, and the time complexity is O (n^2).

Selecting the first element as the benchmark is relatively simple, but when it is the maximum or minimum, the efficiency of quick sorting will be reduced. At this time, the method of "three items in the middle" is used to avoid the occurrence of the maximum.

Non-recursive implementation

Posted by mikeoffy69 on Tue, 23 Jul 2019 18:51:54 -0700