Exchange class sorting -- bubbling, fast

1. Bubble sort

1. Execution process:

Original sequence: 49, 38, 65, 97, 76, 13, 27, 49

1) Compare No. 1 and No. 2, 49>38, exchange.

    38,49,65,97,76,13,27,49

2) Comparing No. 2 and No. 3, 49<65, no exchange.

    38,49,65,97,76,13,27,49

3) Comparing No. 3 and No. 4, 65<97, no exchange.

    38,49,65,97,76,13,27,49

4) Compare No. 4 and No. 5, 97>76, exchange.

    38,49,65,76,97,13,27,49

5) Compare No. 5 and No. 6, 97>13, exchange.

    38,49,65,76,13,97,27,49

6) Compare No. 6 and No. 7, 97>27, exchange.

    38,49,65,76,13,27,97,49

7) Compare No. 7 and No. 8, 97> 49 , exchange.

    38,49,65,76,13,27,49,97

At this point, the bubble sort is over.

2. Algorithm idea:

Code:

void BubbleSort(int R[],int n)
{
	int i,j,flag;
	int temp;
	for(i = n-1; i >= 1; --i)
	{
		flag = 0;
		for(j = 1;j <= i;++j)
		{
			if(R[j-1] > R[j])
			{
				temp = R[j];
				R[j] = R[j-1];
				R[j-1] = temp;
				flag = 1 //If no swap occurs, flag is 0; if swap occurs, flag is changed to 1
			}
			if(flag == 0)
				return;
		}		
	}
}

3. Complexity analysis:

(1) Time complexity analysis
     By the code of the bubble sort algorithm, the keyword exchange operation in the innermost loop can be selected as the basic operation. 1) In the worst case, that is, the entire sequence is reversed, the condition R[j]<R[j-1]
    of the if statement in the inner loop is always true, and the number of times the basic operation is performed is ni. The value of i is 1~n-1. The total number of executions is (n-1+1)(n-1)/2=n(n-1)/2, and the time complexity is O(n²) .

2) In the best case, that is, the entire sequence has been ordered, then the condition R[j]<R[j-1]     of the if statement in the inner loop is always invalid. The time complexity is O(n) .

    Combining the above two cases, the average time complexity of this algorithm is O(n²).

(2) The space complexity is O(1) .

2. Quick Sort

1. Execution process:

Original sequence: 49 , 38, 65, 97, 76, 13, 27, 49

                  i j (i and j start with head and tail keywords respectively)

For the first quick row, with 49 as the hub, the whole process is a process of alternating scans and exchanges.

1) Using j, scan forward from the far right end of the sequence until it encounters a number 27 smaller than 49 , where j stops.

    49,38,65,97,76,13,27,49

      i                                           j

2) Swap 27 to the position of i at the front end of the sequence.

    27,38,65,97,76,13,    ,49

      i                                          j

3) Using i, change the scanning direction, scan from front to back, until it encounters a number 65 greater than 49 , i stops here.

    27,38,65,97,76,13,    ,49

                    i                            j

4) Swap 65 to the position of the sequence backend j.

    27,38,    ,97,76,13,65,49

                   i                             j

5) Using j, change the scanning direction, scan from back to front, until encountering a number 13 smaller than 49 , j stops here.

    27,38,    ,97,76,13,65,49

                   i                       j

6) Swap 13 to the position of i at the front end of the sequence.

    27,38,13,97,76,    ,65,49

                    i                     j

7) Using i, change the scanning direction, scan from front to back, until it encounters a number 97 greater than 49 , i stops here.

    27,38,13,97,76,    ,65,49

                            i             j

8) Swap 97 to the position of the front j of the sequence.

    27,38,13,     ,76,97,65,49

                            i              j

9) Using j, change the scanning direction, and scan from back to front until a number smaller than 49 is encountered . When i and j meet, the scan ends.

    27,38,13,    ,76,97,65,49

                            ij

10) The position where i is equal to j is the final position of 49 . The first quick sort is over.

    27,38,13,49,76,97,65,49

                            ij

    Next, sort the sequences {27, 38, 13} and {76, 97, 65, 49} in the same way.

2. Algorithm idea:

Code:

void QuickSort(int R[],int low,int high) //sort the keywords from R[low] to R[r]
{
	int temp;
	int i = low, j = r;
	if(low < high)
	{
		temp = R[low];
		while(i != j)
		{
			while(i<j && R[j]>=temp)
				--j; //Scan from right to left to find a keyword less than temp
			if(i < j)
			{
				R[i] = R[j];
				++i;
			}
			while(i<j && R[i]<temp)
				++i; //Scan from left to right to find a keyword greater than temp
			if(i < j)
			{
				R[j] = R[i];
				--j;
			}
		}
		R[i] = temp; // put temp in final position
		QuickSort(R,low,i-1); //Recursively sort the keywords on the left of temp
		QuickSort(R,i+1,high); //Recursively sort the keywords to the right of temp
	}
}

3. Complexity analysis:

(1) Time complexity: O(nlogn) in the best case, the closer the sequence to be sorted is to the disorder, the higher the algorithm efficiency;

                             The worst case is O(n²), the closer the sequence to be sorted is, the less efficient the algorithm is.

(2) The space complexity is O( logn ) .

        Quick sort is performed recursively, and recursion requires the assistance of the stack, so it requires a large auxiliary space.




Guess you like

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