第10课。
This commit is contained in:
43
quickSort.c
Normal file
43
quickSort.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
int num[100];
|
||||
|
||||
void quickSort(int numbers[], int leftBound, int rightBound)
|
||||
{
|
||||
int baseline = numbers[leftBound];
|
||||
int leftPos = leftBound, rightPos = rightBound;
|
||||
if (leftBound < rightBound)
|
||||
{
|
||||
while (leftPos < rightPos)
|
||||
{
|
||||
while (numbers[rightPos] > baseline && rightPos > leftPos)
|
||||
{
|
||||
rightPos--;
|
||||
}
|
||||
numbers[leftPos] = numbers[rightPos];
|
||||
while (numbers[leftPos] < baseline && leftPos < rightPos)
|
||||
{
|
||||
leftPos++;
|
||||
}
|
||||
numbers[rightPos] = numbers[leftPos];
|
||||
}
|
||||
numbers[leftPos] = baseline;
|
||||
quickSort(numbers, leftBound, leftPos - 1);
|
||||
quickSort(numbers, rightPos + 1, rightBound);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int count = 0, i = 0;
|
||||
scanf("%d", &count);
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
scanf("%d", &num[i]);
|
||||
}
|
||||
quickSort(num, 0, count - 1);
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
printf("%d ", num[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user