数据结构与算法(八) · 5 目录页 张铭《数据结构与算法》 内排序 8.7.2 排序算法的理论和实验时间 第八章 8.7.2 排序算法的理论和实验时间
Sort( 排序 )
-
Upload
imani-patel -
Category
Documents
-
view
175 -
download
2
description
Transcript of Sort( 排序 )
-
Sort():
-
OutlineSortInternal SortingBubble SortSelection SortInsertion SortQuick SortHeap SortExternal SortingMerge Sort
-
Sort():Internal Sorting() External Sorting()
-
Internal Sorting---Bubble Sort:: 26, 5, 81, 7, 63 Step1: 5, 26, 7, 63, 81Step2: 5, 7, 26, 63, 81Step3: 5, 7, 26, 63, 81Step4: 5, 7, 26, 63, 81why?4
-
Internal Sorting---Bubble Sort Example().
void main( ) { int i,j,tmp; int a[5] = {26, 5, 81, 7, 63 }; for ( i = 0; i < 4; i++ ) { for ( j = 0; j < 4; j++ ) { if (a[j] > a[j+1]) { tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } } printf(Loop %d : ,i); for ( j = 0; j < 5; j++ ) printf(%4d,a[j]); printf(\n); system(pause); } }O(n2), why?:nn-1(n-1) (n-2)21n(n-1)/2.
-
Internal Sorting---Bubble Sort Examplea[0] a[1] a[2] a[3] a[4]i=0 j=0
j=1
j=2 j=3a[0] a[1] a[2] a[3] a[4]i=1 j=0j=1
j=2
j=3
a[0] a[1] a[2] a[3] a[4]i=2j=0
j=1
j=2
j=3a[0] a[1] a[2] a[3] a[4]i=3j=0
j=1
j=2
j=3
52681763
52678163
52676381
57266381
57266381
57266381
57266381
57266381
57266381
57266381
57266381
57266381
-
Internal Sorting---Selection Sort
-
Internal Sorting---Selection Sort: l, k, j, o, a, b .O(n2)Bubble Sort.
lkjoab
abjolk
abjklo
abjklo
-
Selection Sort Examplevoid select(char *string,int count){ int pos; /**/ int i,j; char temp; for ( i = 0; i < count - 1; i++ ) /*,*/ { pos = i; temp = string[pos]; for ( j = i + 1; j < count; j++ ) /*,*/ if ( string[j] < temp ) /**/ { pos = j; /**/ temp = string[j]; } string[pos] = string[i]; /**/ string[i] = temp; printf(": [%s]\n",string); /* */ }}
-
Internal Sorting---Insertion Sort::
-
Internal Sorting---Insertion Sort: l, k, j, o, a, b O(n2),why?n-11,2,3n-2,n-1n(n-1)/2.
kljoab
lkjoab
jkloab
jkloab
ajklob
abjklo
-
Insertion Sort Example#include #define MAX 20 /**/void insert(char *string,int count){ int i,j; char temp; for ( i = 1; i < count; i++ ) /* ,*/ { temp = string[i]; /**/ j = i - 1; /**/ /*,*/ while ( j >= 0 && temp < string[j] ) { string[j+1] = string[j]; j--; } string[j+1] = temp; /**/ printf(": [%s]\n",string); /**/ }}
-
Quick Sort()..
-
Quick Sort0 1 2 3 4 5 6 7ij[1],[5]jiij,j
-
Quick Sort
abcdefgh
-
Quick Sortvoid q_sort(char *string,int left,int right){ char partition; /**/ char temp; int i,j,k;
if ( left < right ) /**/ { i = left; /**/ j = right + 1; /**/ partition = string[left]; /**/ do { do { /*,par*/ i++; } while( string[i] < partition ); do { /*,par*/ j--; } while( string[j] > partition ); if ( i < j ) { temp = string[i]; /*,*/ string[i] = string[j]; string[j] = temp; } } while( i < j ); temp= string[left]; /*i>=j jleft*/ string[left] = string[j]; string[j] = temp; printf(": "); for ( k = left; k
-
Quick Sortvoid quick(char *string,int n){ q_sort(string,0,n-1);}void main(){ char string[MAX]; /**/ int count; /**/ printf(" ==> "); gets(string); /**/ count = strlen(string); /**/ quick(string,count); /**/ /**/ printf("\n: [%s]\n",string);}
-
Heap SortHeap,:,Heap:Step1:heap.Step2:heap,heap.Step3:.
-
Heap Sort:56824713(2)(3)(4)(5)(6)(7)(8)9(9)(1): 1 2 3 4 5 6 7 8 91, n2*n,2*n+1.
-
Heap Sort---.n,n/2.,,.:Step1:,.(1),.(2),,Step1.
-
56824713(2)(3)(4)(5)(6)(7)(8)9(9)(1)(1)4(9/2).,.56924713(2)(3)(4)(5)(6)(7)(8)8(9)(1)(2)9,.
-
56924713(2)(3)(4)(5)(6)(7)(8)8(9)(1)(3)3.,.56927413(2)(3)(4)(5)(6)(7)(8)8(9)(1)(4)7,.
-
56927413(2)(3)(4)(5)(6)(7)(8)8(9)(1)(5)2.,.59627413(2)(3)(4)(5)(6)(7)(8)8(9)(1)(6)4,,8,.
-
59827413(2)(3)(4)(5)(6)(7)(8)6(9)(1)(7)1.,.95827413(2)(3)(4)(5)(6)(7)(8)6(9)(1)(8)2,,,.
-
98527413(2)(3)(4)(5)(6)(7)(8)6(9)(1)(9)4.,.98627413(2)(3)(4)(5)(6)(7)(8)5(9)(1)(10)9,.
-
Heap Sort #include /* */void adjust_heap(int *heap,int root,int len){ int done; /* */ int j; int temp;
j = 2 * root; /* */ temp = heap[root]; /* */ done = 0; /* */ while ( j = heap[j] ) /* */ done = 1; /* */ else { heap[j/2] = heap[j]; /* */ j = 2 * j; /* */ } } heap[j/2] = temp; /* */}/* */void heap(int *heap,int len){ int i,j,temp;
for ( i = ( len / 2 ); i >= 1; i-- ) /**/ adjust_heap(heap,i,len); printf("\n: "); for ( j = 1; j < 10; j++ ) /* */ printf("[%d]",heap[j]); printf("\n"); /* */ for ( i = len - 1; i >= 1; i-- ) /**/ { temp = heap[i+1]; /*:heap[1]heap[i+1]*/ heap[i+1] = heap[1]; heap[1] = temp; adjust_heap(heap,1,i); /* */ printf("\n: "); for ( j = 1; j < 10; j++ ) /**/ printf("[%d]",heap[j]); }}
-
Heap Sort/* : . */int main(){ /**/ int data[10] = { 0, 5, 6, 4, 8, 2, 3, 7, 1, 9 }; int i;
printf(": "); for ( i = 1; i < 10; i++ ) /* */ printf("[%d]",data[i]); heap(data,9); /* */ printf("\n\n: "); for ( i = 1; i < 10; i++ ) /* */ printf("[%d]",data[i]); printf("\n"); system("pause"); return 0;}
-
Heap Sort Result
-
Merge Sort.Step1:,,,.Step2:Step1,,,.
-
Merge Sort1234567
-
Merge Sort12
-
Merge Sort
void merge(FILE *merge,FILE *sort1,FILE *sort2,int len){ int s1=0,s2=0; /* */ char c,c1,c2; c1 = getc(sort1); /* */ c2 = getc(sort2); /* */ while ( 1 ) { if ( c1 < c2 ) /* */ { /* , */ putc(c1,merge); s1++; if ( s1 < len ) /* */ c1 = getc(sort1); /* */ else break; /* , */ } else { /* , */ putc(c2,merge); s2++; if ( s2 < len ) /* */ c2 = getc(sort2); /* */ else break; } } /*, */ /* */ if ( s1 < len ) { putc(c1,merge); /* */ s1++; /* */ } /* */ if ( s2 < len ) { putc(c2,merge); /* */ s2++; } /* */ while ( s1 < len ) /* */ { c = getc(sort1); /* */ putc(c,merge); /* */ s1++; } /* */ while ( s2 < len ) /* */ { c = getc(sort2); /* */ putc(c,merge); /* */ s2++; }}
-
Merge Sort/**/#include #define LEN 4 /* *//* : , . */int main(){ FILE *fp; /* */ FILE *fp1; /* */ FILE *fp2; /* */
fp = fopen("result.txt","r+"); /* */ if ( fp == NULL ) printf("! \n"); else fp1 = fopen("sort1.txt","r+"); /**/ if ( fp1 == NULL ) printf("! \n"); else { fp2 = fopen("sort2.txt","r+"); /**/ if ( fp2 == NULL ) printf("! \n"); else { printf(", . . . \n"); merge(fp,fp1,fp2,LEN); /**/ printf("! \n"); fclose(fp); /* */ fclose(fp1); fclose(fp2); } } system("pause"); return 0; }:sort1.txt,sort2.txt,result.txt
: 1darray6.c
: Loop 0 : 6 15 13 27 33 Loop 1 : 6 13 15 27 33 Loop 2 : 6 13 15 27 33 Loop 3 : 6 13 15 27 33
: , .
: . : Borland C++(P.204)Sort1.txt1456Sort2.txt2378