Sorting Algorithm (1)--Bubble Sort & Quick Sort

It has been a long time since I wrote an algorithm, and my head is rusted. .

 

First, there are four kinds of sorting: 

      Exchange sort: including bubble sort, quick sort.

      Selection sort: including direct selection sort, heap sort.

      Insertion sort: Including direct insertion sort, Hill sort.

      Merge Sort: Merge Sort.

 

This paper studies the exchange ordering.

 

1. Bubble sort

  1. Compare the two adjacent data before and after, if the former data is greater than the latter data, the two data will be exchanged.
  2. In this way, after traversing the 0th data to N-1 data of the array once, the largest data "sinks" to the N-1th position of the array.
  3. N=N-1, if N is not 0, repeat the previous two steps, otherwise the sorting is completed.

The essence: compare and exchange between adjacent

 

 

public static void bubbleSort(int arr[]){
	for(int i=0;i<arr.length;i++){//1. Outer loop
		for(int j=0;j<arr.length-1-i;j++){//2. inner loop
			if(arr[j]>arr[j+1]){//3. Compare one by one, swap if greater than
				int temp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = temp;
			}
		}
	}
}

 

 

2. Quick sort

The idea used in quicksort is the divide and conquer idea.

Quick sort is to find an element (in theory, you can find any one) as the pivot, and then partition the array so that the value of the element on the left of the benchmark is not greater than the benchmark value, and the value of the element on the right side of the benchmark is not less than the benchmark value. , so that the element as the reference is adjusted to the correct position after sorting. Recursive quicksort, adjust the other n-1 elements to the correct position after sorting. Finally each element is in the correct position after sorting and the sorting is complete. So the core algorithm of the quicksort algorithm is the partition operation, that is, how to adjust the position of the datum and adjust the final position of the returned datum for divide and conquer recursion.

 

  1. Record the left coordinate of the sorted array, the right coordinate of the current sorted array, and take the data on the left as the base point (dig a hole for this point)
  2. Look from the right to the left, find a data smaller than the base, and fill the data found from the right that is smaller than the base into the hole dug on the left
  3. Search from left to right, find a data larger than base, and fill the data larger than base found from the left into the hole dug on the right
  4. Fill the base to the last remaining hole
  5. Sorts the array on the left of the sorted array that depends on base
  6. Sorts the array on the right of the sorted array that depends on base

Too abstract. . .

Let me give an example

(1), left=0, right=5, base=50;//First record the coordinates of the left and the right, and take the first data as the benchmark. It is understandable that array[0] has been dug.

0
1
2
3
4
5
50
20
10
70
30
60
(2) Search from right to left, find a data smaller than the benchmark 50, the data is 30, at this time, the array [0]=30 is filled, and the array [4] is dug. The right coordinate is moved to 4, and the left is still 0.
0
1
2
3
4
5
30
20
10
70
30
60
(3) Search from left to right, find a data larger than the benchmark 50, the data is 70, at this time, the array [4]=70, and the array [3] is dug. The left coordinate is moved to 3, and the right is still 4.
0
1
2
3
4
5
30
20
10
70
70
60
(4) Continue to perform steps 2 and 3 until left==right, then left==right==3, and return the last remaining pit to the base
0
1
2
3
4
5
30
20
10
50
70
60
(5) At this time, the data on the left side of the array [3] is smaller than the base, and the data on the right side of the array [3] is larger than the base, and then the above loop operation is performed on the array [0~2], and the array [4] ~5] to do the above. until all data is sorted.
 
 

code show as below:  

public static void quickSort(int array[],int sortArrayLeft,int sortArrayRight){
	if(sortArrayLeft>=sortArrayRight){
		return;
	}
	
	int left = sortArrayLeft; //1.1, the left coordinate of the current sorted array
	int right = sortArrayRight; //1.2, the right coordinate of the current sorted array
	int base = array[left]; //1.3, take a data on the left as the base point
	
	while(left<right){
		while(left<right && array[right]>=base){//2, search from the right to the left, and find a data smaller than base
			right --;
		}
		
		array[left] = array[right];//3. Fill in the data less than base found from the right to the hole dug on the left
		
		while(left <right && array[left] <= base){//4, search from left to right and find a data larger than base
			left ++;
		}
		
		array[right] = array[left];//5. Fill in the data larger than base found from the left to the hole dug on the right
	}
	
	array[left] = base;//6, fill the base to the last remaining pit
	
	quickSort(array, sortArrayLeft, left-1);//7, sort the array on the left of the sorted array that depends on the base
	quickSort(array, left+1, sortArrayRight);//8, sort the array on the right of the sorted array that depends on the base
}

 

 

 

Guess you like

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