Algorithm การเรียงข้อมูล (Sorting)

Algorithm Selection Sort   Simulation


Selection_Sort(A[], Max)

{ for i=1 to Max-1 for j=i+1 to Max if(A[i]>A[j]) swape(A[i],A[j]); }

Algorithm Selection Sort (แบบที่สอง)


Selection_Sort(A[], Max)

{ for i=1 to Max-1 { l=i; for j=i+1 to Max if(A[l]>A[j]) l=j; if(l<>i) swape(A[l],A[i]); } }

Algorithm Bubble Sort (ดันมาด้านหลัง)   Simulation


Bubble_Sort(A[], Max)

{ for i=Max-1 downto 1 for j=1 to i if(A[j]>A[j+1]) swape(A[j],A[j+1]); }

Algorithm Bubble Sort (ดันมาด้านหน้า)


Bubble_Sort(A[], Max)

{ for i=2 to Max for j=Max downto i if(A[j-1]>A[j]) swape(A[j-1],A[j]); }

Algorithm Bubble Sort (แบบที่สอง)


Bubble_Sort(A[], Max)

{ n=Max-1; sort=1; while(n>=1 && sort) { sort=0; for j=1 to n if(A[j]>A[j+1]) { swape(A[j],A[j+1]); sort=1; } n=n-1; } }

Algorithm Insertion Sort   Simulation


Insertion_Sort(A[], Max)

{ for i=2 to Max { flag=j=1; while(j<i && flag) { if(A[i]=<A[j]) { temp=A[i]; for n=i downto j+1 A[n]=A[n-1]; A[j]=temp; flag=0; } j++; } } }

Algorithm Insertion Sort (แบบที่สอง)  Document


Insertion_Sort(A[], Max)

{ for i=2 to Max { j=i; sort=1; while(j>1 && sort) { if(A[j-1] > A[j]) swape(A[j],A[j-1]); else sort=0; j=j-1; } } }

Algorithm Shell Sort   Document


Shell_Sort_Stupid(A[], Max)

{ Pir = Max; Do { Leng = Pir = (Pir /3)+1; n = Max – Leng; for (Lop=1 to n Step Leng) { for (i=n downto 1 Step Leng) { for (J:=lop to n Step Leng) { if(A[J] > A[J+Leng]) Swape(A[J],A[J+Leng]); } } } } while(Pir > 0); }

Shell_Sort(A[], Max)

{ Pir=Max; do { Pir = Pir/3; leng=Pir+1; UpLim = Max - leng; for n=1 to leng { sort = 'y'; Up = UpLim; while(Up>=1 && sort=='y') { sort='n'; for j=n to Up step leng { if(A[j]>A[j+leng]) { swape(A[j],A[j+leng]); sort='y'; } } Up=Up-leng; } } } while(Pir>0); }

Algorithm Bucket Sort


Bucketl_Sort(A[], Max, Leng)

{ for i=1 to Max Bucket[i]=-1; for i=1 to Leng Bucket[A[i]]=A[i]; j=0; for i=1 to Max if (Bucket[i]>-1) A[++j]=Bucket[i]; }

Algorithm Bucket Sort (แบบซ้ำค่า)


Bucketl_Sort(A[], Max, Leng)

{ for i=1 to Max Bucket[i]=0; for i=1 to Leng Bucket[A[i]]++; j=0; for i=1 to Max while (Bucket[i] > 0) { A[++j]=i; Bucket[i] - -;} }

Algorithm Radix Sort  


RadixSort(A[],flg,Base,MAX)

{ for i=1 to 9 { new(pocket[i]); tail[i]=pocket[i]; } flg='no'; dumy=Base*10; for i=1 to MAX { n=(A[i] MOD Base)/(Base/10); new(temp); temp->infor=A[i]; tail[n]->next=temp; tail[n]=temp; if(A[i]>dumy) flg='yes'; } n=-1; for i=1 to 9 { temp=pocket[i]->next; while(temp!=nul) { temp1=temp; A[++n]=temp->infor; temp=temp->next; free(temp1); } free(pocket[i]); } if(flg='yes') RadixSort(A,flg,Base=dumy,MAX); }

Algorithm Quick Sort   Simulation  Document


Partition(A[],l,u,j)

{ i=l+1; j=u; while(i=<j) { if(A[i]=<A[l]) i=i+1; else { while(A[j]>A[l]) j=j-1; if(j>i) swape(A[i],A[j]); } } swape(A[j],A[l]); return(A[],j); }

MainQuick(A[],l,u) //not use recuresive

{ top=1; Push(l,u); while(top>0) { pop(l,u) if(l<u) { Partition(A,l,u,j); Push(l,j-1); Push(j+1,u); } } }

MainQuick(A[],l,u) //use recuresive

{ if(l<u) { Partition(A,l,u,j); MainQuick(A,l,j-1); MainQuick(A,j+1,u); } }

Algorithm Heap Sort   Document


Setup(A[],Max) //Create Heap

{ for q=2 to Max { i=q; j=i/2; while(i>1 && A[i]>A[j]) { swape(A[i],A[j]); i=j; j=i/2; } } }

Setdown(A[],Max) //Sorting

{ for q=Max Downto 2 { swape(A[1],A[q]); i=1; j=i*2; if (A[j+1]>A[j] && j+1=<q-1) j++; while(j=<q-1 && A[j]>A[i]) { swape(A[i],A[j]); i=j; j=i*2; if (A[j+1]>A[j] && j+1=<q-1) j++; } } }

Algorithm Merge Sort


Splite file . . . ibegin=0; do { iend=ibegin+pir; if(iend>Max) iend=Max; SpliteFile(ibegin,iend); ibegin=iend+1; } while(iend<=Max); . . .

MergeSort(fa,fb,fc)

{ read(dataA from fa, dataB from fb); do { if(dataA<dataB) { store(dataA into fc); read(dataA from fa); } else if(dataA>dataB) { store(dataB into fc); read(dataB from fb); } else { store(dataA into fc, dataB into fc); read(dataA from fa, dataB from fb); } } while(fa not EOF && fb not EOF); }

Algorithm External Sort   Simulation