Home > Back-end >  Virtual judge on a quick sort, I use c program, VS the normal operation, the virtual display on judg
Virtual judge on a quick sort, I use c program, VS the normal operation, the virtual display on judg

Time:09-30

The code is as follows:
#include
#include

Int a, [100000].

Void QuickSort (int a [], int p, int r);
Int Partition (int a [], int p, int r);
Void Swap (int a [], int, int j);

Int main () {
Int N, m, I;
The scanf (" % d ", & amp; N);
for(i=0; IThe scanf (" % d ", & amp; M);
A [I]=m;
}
A, QuickSort (0, N - 1);
for(i=0; IPrintf (" % d ", a [I]);
printf("\n");
return 0;
}
Void swep (int a [], int, int j) {
Int temp=a [I];
A [I]=a, [j].
A [j]=temp;
}

Int Partition (int a [], int p, int r) {
Int I=p;
Int j=r + 1;
Int key=a, [p].
While (1) {
While (a [-] j & gt; The key & amp; & I & lt; R);
While (a [+ + I] If (I & gt;=j)
break;
Swep (a, I, j);
}
A [p]=a, [j].
A [j]=key;
Return j;
}

Void QuickSort (int a [], int p, int r) {
If (pInt pos=Partition (a, p, r);
QuickSort (a, p, pos - 1);
QuickSort (a, pos + 1, r);
}
}

CodePudding user response:

Int a, [100000].
This array defines whether a bit big, try get smaller

CodePudding user response:

But the topic request n< array element number; 100000

CodePudding user response:

While (a [-] j & gt; The key & amp; & I & lt; R);//@ 1
While (a [+ + I]
Change
While (a [-] j & gt; Key) {if (j==p) break; }
While (a [+ + I]
=========
@ 1 first, two wrong: to analyze the following
1. Because is for j, the decrement, you cross the border to prevent j, this is not a judgment I & lt; R, should be more than p, I cannot be more than the left boundary
2. Even to this while (a [-] j & gt; The key & amp; & J & gt;=p; Like this? Do you think OK?
Still have problem, assuming j==p, while at this time will continue to go once, walked to the front half, the next whlie conditions (a [-] j & gt; The key), j and crossing the line
Then change to the while (a [-] j & gt; The key & amp; & J & gt; P); How to? OK,

  • Related