Implement sorting of an array with only 0, 1, and 2

Question: There are only three elements in an array, 0, 1 , and 2, and it is required to sort such an array.

1. Ideas:

1.1 Idea 1:

  The first time you see such a problem, it will be very simple, you only need to traverse the array twice to complete. The first traversal is to scan the elements in the array. Every time 0 is encountered, count0++, 1 is count1++, and 2 is count2++. In this way, the number of 0, 1, and 2 in the array can be counted. . Then in the second traversal, you only need to reassign the array, and assign count0 0s, count1 1s, and count2 2s from the beginning. Finally complete the sorting of the array.

1.2 Idea 2:

  Since it is an interview question, it will definitely not let you solve it so easily. The interviewer said what if the join can only be traversed once, and then you don't know.

  If this problem can only be traversed once, we will definitely think of using multiple pointers. I've come across this problem a lot before. Similar to the halved search, two pointers need to be set, but this problem requires three pointers, respectively pointing to the end of the three elements 0, 1, and 2 in the array. Add a sorted array {0,0,1,1,2,2}, then p0 points to the 0 with the subscript 1, p1 points to the 1 with the subscript 3, and p2 points to the subscript 5 that 2.

p0 and p1 scan from front to back, p2 scans from back to front,

When initializing:

p0 points to the first non-zero element, then arry[p0]=1||2

p1 points to the first non-1 element, then arry[p1]=0||2

p2 points to the first non-2 element, then arry[p2]=0||1

if:

arry[p0]==2, arry[p2]==0, swap two elements

arry[p1]==2, arry[p2]==1, swap two elements

arry[p0]==1, arry[p1]==0, swap two elements

Otherwise, it is only possible that the three numbers pointed to by p0, p1, and p2 are different, then the following assignments are made

arry[p0]==0,arry[p1]==1,arry[p2]==2。

If i>k occurs after the above swap, k=i. (PS: 2012-10-5)

2. Code example

copy code
#include<iostream>
#include<stdlib.h>
using namespace std;

void PrintArry(int arry[],int len)
{
    for(int i=0;i<len;i++)
        cout<<arry[i]<<" ";
    cout<<endl;
}

void swap(int arry[],int i,int j)
{
    int temp=arry[i];
    arry[i]=arry[j];
    arry[j]=temp;
}

void sort(int arry[],int len)
{

    int i= 0 ; // head pointer points to 0 
    int j = len - 1 ; // tail pointer points to 2 
    int k = 0 ; // middle pointer points to 1 
    while (i <= j && k <= j && i <= k)
    {
        // i points to the first non-zero value 
        while (arry[i] == 0 ){
            i++;
        }
        // k points to the first non-1 value 
        while (arry[k] == 1 ){
            k++;
        }
        // j points to the first non-2 value 
        while (arry[j] == 2 ){
            j--;
        }

        if(i <= j && arry[j] == 0 && arry[i] == 2){
            swap(arry,i,j);
            i++;
            j--;
        }

        else if(k <= j && arry[k] == 2 && arry[j] == 1){
            swap(arry,k,j);
            k++;
            j--;
        }

        else if(i <= k && arry[k] == 0 && arry[i] == 1){
            swap(arry,k,i);
            i++;
            k++;
        }

        else if(i < k && k < j){
            arry[i] = 0;
            arry[k] = 1;
            arry[j] = 2;
            i++;
            k++;
            j--;
        }

        if(i>k)
        {
            k=i;
        }
    }
}

void main()
{
    int arry[]={1,0,2,2,0,1,0,1};
    int len=sizeof(arry)/sizeof(int);

    PrintArry (arry, len);
    sort(arry,len);
    PrintArry (arry, len);

    system("pause");
}                
copy code
 
Simplified idea:

经试验sort函数好像还可以精简
void sort(int arry[],int len)
{

int i= 0;//头指针指向0
int k = 0;//中间指针指向1
int j = len - 1;//尾指针指向2
while(k <= j && i <= k)
{
//i指向第一个非0值
while(arry[i] == 0)
i++;
//k指向第一个非1值
while(arry[k] == 1)
k++;
//j指向第一个非2值
while(arry[j] == 2)
j--;
if(i > k)
k = i;

if(i < j && arry[j] == 0 && arry[i] == 2)
{
swap(arry,i,j);
}
else if(k <j && arry[k] == 2 && arry[j] == 1)
{
swap(arry,k,j);
}
else if(i < k && arry[k] == 0 && arry[i] == 1)
{
swap(arry,k,i);
}
else if(i < k && k < j)
{
arry[i] = 0;
arry[k] = 1;
arry[j] = 2;
}

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326920190&siteId=291194637