#include <string>
typedef int ElementType;
typedef struct Squential {
ElementType *Data;
int Length;
} Squential;
void EnterElement(Squential &List) {
int Len;
int x;
printf("Please input the Element Length:");
scanf("%d", &Len);
fflush(stdin);
List.Length = Len;
List.Data = (ElementType *) malloc(sizeof(ElementType) * List.Length);
for (int i = 0; i < List.Length; ++i) {
scanf("%d", &x);
List.Data[i] = x;
}
}
void PrintList(Squential List) {
for (int i = 0; i < List.Length; ++i) {
printf("%4d", List.Data[i]);
}
printf("\n");
}
int QuickAlgorithm(Squential &List, int Low, int High) {
int SplitValue = List.Data[Low];
while (Low < High) {
while (Low < High && SplitValue < List.Data[High]) {
High--;
}
if (Low < High) {
List.Data[Low] = List.Data[High];
}
while (Low < High && SplitValue > List.Data[Low]) {
Low++;
}
if (Low < High) {
List.Data[High] = List.Data[Low];
}
}
List.Data[Low] = SplitValue;
return Low;
}
void SortingFunction(Squential List, int Low, int High) {
int SplitValue;
if (Low < High) {
SplitValue = QuickAlgorithm(List, Low, High);
SortingFunction(List, Low, SplitValue - 1);
SortingFunction(List, SplitValue + 1, High);
}
}
int main() {
Squential List;
EnterElement(List);
PrintList(List);
SortingFunction(List, 0, 9);
PrintList(List);
return 0;
}
C/C++快速排序
最新推荐文章于 2024-06-01 18:28:10 发布