2016 7th Blue Bridge Cup C/C++ Programming Undergraduate Group B Provincial Competition Quick Sort (fill in the blank)

quicksort

Sorting is often used in various situations.
Quick sort is a very common and efficient algorithm.

The idea is: first select a "ruler" and
use it to sieve the entire queue
to ensure that the elements on its left are not larger than it, and the elements on its right are not smaller than it.

In this way, the sorting problem is split into two subintervals.
Then you can sort the subintervals separately.

The code below is an implementation, please analyze and fill in the missing code in the underlined part.
.
#include <stdio.h>  
  
void swap(int a[], int i, int j)  
{  
    int t = a[i];  
    a[i] = a[j];  
    a[j] = t;  
}  
  
int partition(int a[], int p, int r)  
{  
    int i = p;  
    int j = r + 1;  
    int x = a[p];  
    while(1){  
        while(i<r && a[++i]<x);  
        while(a[--j]>x);  
        if(i>=j) break;  
        swap(a,i,j);  
    }  
    _______________;//fill in the blank  
    return j;  
}  
  
void quicksort(int a[], int p, int r)  
{  
    if(p<r){  
        int q = partition(a,p,r);  
        quicksort(a,p,q-1);  
        quicksort(a,q+1,r);  
    }  
}  
      
intmain()  
{  
    int i;  
    int a[] = {5,13,6,24,2,8,19,27,6,12,1,17};  
    int N = 12;  
      
    quicksort(a, 0, N-1);  
      
    for(i=0; i<N; i++) printf("%d ", a[i]);  
    printf("\n");  
      
    return 0;  
}  

A little different from the quick sort on the previous data structure, this question first selects a reference number x (that is, the first element a[p] of the sequence to be sorted ). The left scan is performed first. When a[i] is less than it, the condition is met. When a[i] is greater than or equal to it, the condition is not met, and the right scan is performed. When the right scan is performed, a[j] is also compared with x. , when a[j]<=a[i] does not meet the condition, then if i<j is satisfied, the positions of a[i] and a[j] are exchanged. Repeat the operation continuously until i==j, and jump out of the loop. At the same time, the first element (axis value x) and a[j] are exchanged, which is to divide the column to be sorted into two parts, the value of the first part is less than or equal to x, and the value of the latter part is greater than or equal to x.

.
.
Completion code:
    
#include<stdio.h>
void swap(int a[], int i, int j)
{
	int t = a[i];
	a[i] = a[j];
	a[j] = t;
}
int partition(int a[], int p, int r)
{
	int i = p;
	int j = r + 1;
	int x = a[p];
	while (1) {
		while (i<r && a[++i]<x);//i is compared with x
		 while (a[--j]>x);//j is also compared with x
	     	if (i >= j)
			   break;
		   swap(a, i, j);
	}
	swap(a, p, j);//This is the last step: jump out of the loop when i==j, and at this time a[j] is greater than x (x is the last element of the sequence to be sorted), because Quick sort must put the big ones later, so the last value and j are exchanged.
	return j;
}
void quicksort(int a[], int p, int r)
{
	if (p<r) {
		int q = partition(a, p, r);
		quicksort(a, p, q - 1);
		quicksort(a, q + 1, r);
	}
}
intmain()
{
	int i;
	int a[] = { 5,13,6,24,2,8,19,27,6,12,1,17 };
	int N = 12;

	quicksort(a, 0, N - 1);

	for (i = 0; i<N; i++)
		printf("%d ", a[i]);
	printf("\n");

	return 0;
}

.
.


Guess you like

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