Quick sort (digging method)

quick sort order

- Implementation

There are roughly two methods:
1. Left and right pointer method
2. Digging method

First of all forleft and right pointer, friends who want to know, you can go to another link of mine: left and right pointer method .
next thing ispit digging

The basic idea of ​​digging pit method

  • The idea of ​​digging holes andleft and right pointerThe idea is similar:

1. Select a piece of data (usually the leftmost or rightmost) and store it in the key variable, forming a pit at the data location.
2. Still define a left and a right, L walks from left to right, R walks from right to left. (If you dig a pit on the far left, you need to go first by R; if you dig a pit on the far right, you need to go first by L)

The following idea is similar to the idea of ​​(left and right pointer method), so I won’t talk about it here:
see the diagram:

If you want to sort an array:
——
insert image description here

If you find out that the first digit on the left is put into the key variable, leaving a hole:
insert image description here

Then there is right, and the position pointed to by left:
insert image description here

Start to move, right first searches to the left step by step until a number smaller than key is found;
insert image description here

Then fill in the pit with the value found by right:

insert image description here

  • Then left starts to find a number larger than key step by step to the right, and then fills it into the pit pointed by right:
    insert image description here

Then repeat the above operations until right and left meet, and let the value of key be filled in this pit.

insert image description here

At this time, 3 has been filled in the correct position, and then the left and right sides of 3 are divided into two arrays;
first look at the array on the left of 3:
insert image description here

The next step is the same as before:
find the first digit on the left and put it into the key variable. At this time, right moves one step to the left, then let left and right meet, and the key fills the pit again:
but put 1 into the correct There is a value left after the position, then this value is in the correct position;

Note: Stop the operation until there is only one data in the left and right sequences, or the left and right sequences do not exist, which means that this part is in order at this time.

Look again at the value to the right of 3:
insert image description here
the same manipulation as:
insert image description here

To fill the hole for the first time:

insert image description here

  • Left finds a value greater than key and fills it in the pit:

insert image description here
Then right moves to the left, will meet left again, and fill the key into the pit:
insert image description here
at this time, 6 is placed in the correct position:
again, the left and right sides of 6 are regarded as two arrays, and they are sorted:
the same as above Steps:
sort the array at the end;
insert image description here
code implementation:

void QuickSort(int *arr,int begin,int end)
{
    
    
	if (begin >= end)
		return;
	int left = begin;
	int right = end;
	int key = arr[begin];

	while (begin < end)
	{
    
    
		//找小
		while (arr[end] >= key && begin < end)
		{
    
    
			--end;
		}
		//小的放到左边的坑里
		arr[begin] = arr[end];
		//找大
		while (arr[begin]<=key && begin<end)
		{
    
    
			++begin;
		}
		//大的放到右边的坑里
		arr[end] = arr[begin];

	}
	//将key填入坑中
	arr[begin] = key;
	int keyi = begin;

	QuickSort(arr, left, keyi - 1);
	QuickSort(arr, keyi + 1, right);
}

int main()
{
    
    
	int arr[8] = {
    
     3,2,4,6,5,7,9,1 };

	int left = 0;
	int right = 7;

	QuickSort(arr, left,right);


	for (int i = 0; i < 8; i++)
	{
    
    
		printf("%d ", arr[i]);
	}

	return 0;
}

——Finally
, thanks to all the friends who watched: ^ _ ^

Guess you like

Origin blog.csdn.net/m0_66780695/article/details/131282543