第10课。

This commit is contained in:
unlockable
2022-11-25 23:41:25 +08:00
parent 605137a437
commit a37a95fbed
7 changed files with 188 additions and 0 deletions

43
quickSort.c Normal file
View 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;
}