Leetcode912. Sorting array (three-way division)


1. Three-way division

Why is there still a three-way division?

The quick sort algorithm is extremely inefficient when a large amount of data is repeated, and the time limit will be exceeded when running the program. In order to solve the problem of large amount of data duplication, three-way division was born. The three-way division is based on the idea of ​​quick sorting. The three-way division is designed to solve a large number of repetitions or almost the same data in the data, but it can also be sorted for general data.

three way division

The three-way division is based on the idea of ​​quick sorting, selecting keywords in the data (selecting keywords randomly), and then sorting the keywords (keys) in a single pass, so that values ​​smaller than the key are thrown to the left of the key, Throw values ​​larger than the key to the right, and the values ​​in a large part of the middle interval are the same. There is no need to process the equal intervals in the middle. The left interval of this keyword is processed in the same way, and the right interval is processed in the same way, and the equal value interval is no longer processed.
insert image description here

Three-pointer implementation, left,cur,rightif the selected keyword is randomly selected, if the left side is used as the keyword key=a[left], then go from the left. At this time cur = left+1, if the right side is used as the keyword key = a[right] after random selection, then cur = right- 1, Start walking from the right. The corresponding value of cur is compared with the keyword key. When a[cur] == key,cur++; when a[cur] < key, a[cur] and a[left] are compared. At this time cur++,left++, when a[cur] > key,将a[right]与a[cur]交换, at this time right--, cur does not move, because it is not known whether the exchanged value is greater than or Less than or equal to the keyword key, until cur goes beyond right. In such a row, the interval will be obtained right - left中的值是相同. The interval [begin,left]value is smaller than the key, and the interval [right+1,end]value is greater than the key. At this time, it is divided into three intervals (less than, equal to, greater than). This method is also very similar to the front and back pointer method of quick row.
insert image description here
After a row, three intervals are divided
insert image description here

2. Leetcode912. Sorting array

insert image description here
insert image description here

The returned array must be malloc by itself

insert image description here

Returns the address of the first element of the array

insert image description here

How big is the returned array

Sort an array using quicksort three-way partition

void Swap(int* e1, int* e2)
{
    
    
	int tmp = *e1;
	*e1 = *e2;
	*e2 = tmp;
}

void QuickSort(int*a,int left,int right)
{
    
    
	//当区间大小为1或者不存在就不需要单趟排序了
    if(left>=right)
    {
    
    
        return ;
    }
	//随机选数
    int randi = left + (rand() % (right - left));
	if (randi != left)
	{
    
    
		Swap(&a[randi], &a[left]);//以左边做关键值
	}

    int begin = left;
    int key = a[left];//以左边做关键值
    int end = right;
    int cur = left+1;//类似前后指针法

    while(cur<=right)
    {
    
    
        if(a[cur] < key)//当前值小于key,交换,两者都向后移动
        {
    
    
            Swap(&a[cur], &a[left]);
            cur++;
            left++;
        }

        else if(a[cur]>key)//大于时与最右边的交换,然后right--,
        //由于最开始并不确定a[left]与key关系,所以交换后cur不动
        {
    
    
            Swap(&a[cur], &a[right]);
            right--;
        }

        else
        {
    
    
            cur++;
        }
    }
    //类似普通快排递归排key值对应区间它的左区间
    QuickSort(a,begin,left-1);
    //递归排key值对应区间它的右区间
    QuickSort(a,right+1,end);
	
	//关键字对应区间不需要处理
}

int* sortArray(int* nums, int numsSize, int* returnSize){
    
    
  int*a = (int*)malloc(sizeof(int)*numsSize);
  *returnSize = 0;
   QuickSort(nums,0,numsSize-1);
   //将排好序的数组依次赋值给malloc出来的数组
   for(int i = 0; i<numsSize;i++)
   {
    
    
       a[(*returnSize)++] = nums[i];
   }
   
   return a;//返回malloc出来的数组a的首地址
}

Three-way partitioning is used to solve the problem of sorting a large amount of repeated data.

Guess you like

Origin blog.csdn.net/m0_67768006/article/details/130225081