#include<stdio.h> #include<string.h> #define LIM 20 #define SIZE 81 #define HALT "" voidstsrt(char *strings [], int num); char * s_gets(char * st, int n); intmain() { char input[LIM][SIZE]; char *ptstr[LIM]; int ct = 0; int k;
printf("Input up to %d lines, and I will sort them.\n", LIM); printf("To stop ,press the Enter key at a line's start.\n"); while(ct < LIM && s_gets(input[ct], SIZE) != NULL && input[ct][0] != '\0') { ptstr[ct] = input[ct]; ct ++; } stsrt(ptstr, ct); puts("\nHere's the sorted list:\n"); for( k = 0; k < ct; k++) puts(ptstr[k]);
return0; }
voidstsrt(char * strings[], int num) { char * temp; int top, seek; for(top = 0; top < num - 1; top++) for(seek = top + 1; seek < num; seek++) if(strcmp(strings[top], strings[seek]) > 0) { temp = strings[top]; strings[top] = strings[seek]; strings[seek] = temp; } }
char * s_gets(char * st, int n) { char * ret_val; int i = 0;
#include<stdio.h> voidbubble_sort(int* p, int len)//函数实现 { int i = 0; int j = 0; for (i = 0; i < len - 1; i++)//需要进行len-1趟 { int flag = 1;//flag=1,说明已经排好序 for (j = 0; j < len - 1 - i;j++)//每趟两两比较较未排好序元素个数-1次。 { if (p[j] > p[j+1]) { int tmp = p[j]; p[j] = p[j + 1]; p[j + 1] = tmp; flag = 0; } } if (flag==1)//判断是否排好序 break; } }
intmain() { int arr[] = { 1,5,9,11,46,79,12 }; int sz = sizeof arr / sizeof arr[0]; bubble_sort(arr,sz); int i = 0; for (i = 0; i < sz; i++) { printf("%d ", arr[i]); } return0; }