更新 'ex8_1.c'

This commit is contained in:
xtaodada 2022-06-10 00:55:54 +00:00
parent a4ec368095
commit 0a03411446

215
ex8_1.c
View File

@ -1,108 +1,107 @@
#include<stdio.h> #include<stdio.h>
#include<stdlib.h> #include<stdlib.h>
#include<conio.h> #include<conio.h>
#define MAX 100 #define MAX 100
typedef int DataType; typedef int DataType;
typedef struct list{ typedef struct list{
int data[MAX]; DataType data[MAX];
int start; int start;
int length; int length;
}List; }List;
/* /*
*/ */
void DirectSort(List *L){ void DirectSort(List *L){
int j; int j;
for(int i = 2; i<=L->length; i++){ for(int i = 2; i <= L->length; i++){
L->data[0] = L->data[i]; L->data[0] = L->data[i];
j = i-1; j = i-1;
while(L->data[0] < L->data[j]){ while(L->data[0] < L->data[j]){
L->data[j+1] = L->data[j]; L->data[j+1] = L->data[j];
j = j-1; j = j-1;
} }
L->data[j+1] = L->data[0]; L->data[j+1] = L->data[0];
} }
} }
/* /*
*/ */
void QuickSort(List *L, int first, int end){ void QuickSort(List *L, int first, int end){
int i = first, j = end, temp = L->data[i]; int i = first, j = end, temp = L->data[i];
while(i < j){ while(i < j){
while(i < j && temp <= L->data[j]){ while(i < j && temp <= L->data[j]){
j--; j--;
} }
L->data[i] = L->data[j]; L->data[i] = L->data[j];
while(i < j && L->data[i] <= temp){ while(i < j && L->data[i] <= temp){
i++; i++;
} }
L->data[j] = L->data[i]; L->data[j] = L->data[i];
} }
L->data[i] = temp; L->data[i] = temp;
if(first < i-1){ if(first < i-1){
QuickSort(L, first, i-1); QuickSort(L, first, i-1);
} }
if(i+1 < end){ if(i+1 < end){
QuickSort(L, i+1, end); QuickSort(L, i+1, end);
} }
} }
/* /*
*/ */
void input(List *L){ void input(List *L){
printf("请输入线性表长度:"); printf("请输入线性表长度:");
scanf("%d", &L->length); scanf("%d", &L->length);
for(int i=L->start; i<L->start + L->length; i++){ for(int i=L->start; i<L->start + L->length; i++){
printf("请输入第 %d 个元素:", i + 1 - L->start); printf("请输入第 %d 个元素:", i + 1 - L->start);
scanf("%d", &L->data[i]); scanf("%d", &L->data[i]);
} }
} }
/* /*
*/ */
void output(List L){ void output(List L){
printf("数组元素为:\n"); printf("数组元素为:\n");
for(int i=L.start; i<L.start + L.length; i++){ for(int i=L.start; i<L.start + L.length; i++){
printf("%d\t", L.data[i]); printf("%d\t", L.data[i]);
} }
printf("\n\n"); printf("\n\n");
} }
int main(){ int main(){
List L; List L;
L.length = 0; L.length = 0;
int choice; int choice;
for(;;){ for(;;){
printf("\t\t1.直接插入排序\n"); printf("\t\t1.直接插入排序\n");
printf("\t\t2.快速排序\n"); printf("\t\t2.快速排序\n");
printf("\t\t0.退出\n"); printf("\t\t0.退出\n");
printf("请选择:"); printf("请选择:");
scanf("%d", &choice); scanf("%d", &choice);
if(choice == 0) break; if(choice == 0) break;
switch (choice) { switch (choice) {
case 1: case 1:
L.start = 1; L.start = 1;
input(&L); input(&L);
output(L); output(L);
DirectSort(&L); DirectSort(&L);
output(L); output(L);
break; break;
case 2: case 2:
L.start = 0; L.start = 0;
input(&L); input(&L);
output(L); output(L);
QuickSort(&L, L.start, L.start + L.length - 1); QuickSort(&L, L.start, L.start + L.length - 1);
output(L); output(L);
break; break;
default: default:
break; break;
} }
printf("请按任意键继续..."); printf("请按任意键继续...");
getch(); getch();
system("cls"); system("cls");
} }
printf("感谢使用,再见\n"); printf("感谢使用,再见\n");
return 0; return 0;
} }