Professional Documents
Culture Documents
19DngBZXEVH3P J7i1-oVMsACyz2wDU2h
19DngBZXEVH3P J7i1-oVMsACyz2wDU2h
Algorithms
Sorting:
Rearranging elements of list so that they are
in ascending or descending order
Sorting Algorithms
■ Exchange Sort
■ Bubble Sort
■ Quick Sort
■ Selection and Tree Sort
■ Straight Selection Sort
■ Binary Tree Sort
■ Heap Sort
■ Insertion Sort
■ Simple Insertion Sort
■ Shell Sort
■ Merge and Radix Sort
■ Merge Sort
■ Radix Sort
Sorting
Bubble Sort
Sorting
Exchanging Code
Object exchange(x,i,j) {
//interchange x[i] and x[j];
temp=x[j];
x[j]=x[i];
x[i]=temp;
return x;
}
Sorting
Bubble Sort (algorithm)
bubble(x,n) {
switch=TRUE;
for (pass=0; pass<n-1 && switch==TRUE; pass++) {
switch=FALSE;
for (j=0; j<pass-1; j++) {
if (x[j]>x[j+1]) {
switch=TRUE;
exchange x[j] and x[j+1];
}
}
}
Sorting
Bubble Sort (Example)
example: 25 57 48 37 12 92 86 33
4th step: 25 48 37 57
12 57
12 92 86 33 x3 > x4 🡪 interchange
6th step: 25 48 37 12 57 86
92 86
92 33 x5 > x6 🡪 interchange
7th step: 25 48 37 12 57 86 92
33 92
33 x6 > x7 🡪 interchange
Sorting
Bubble Sort (Example)
Original data: 25 57 48 37 12 92 86 33
Sorting
Quick Sort (Example)
quick(x,lb,ub) { partition(x,lb,ub,pj) {
if (lb >= ub) return; a=x[lb]; up=ub; down=lb;
partition(x,lb,ub,j); while (down < up) {
quick(x,lb,j-1); while (x[down] <= a && down < ub) down++;
quick(x,j+1,ub); while (x[up] > a) up--;
} if (down < up) exchange x[down] with x[up]
}
x[lb] = x[up]; x[up] = a; *pj = up;
}
a 25 57 48 37 12 92 86 33 lb=0, ub=7, a=x[lb]=25;
down up
12 25 48 37 57 92 86 Sorting
33
Quick Sort (Example)
down up partition(x,lb,ub,pj) {
a=x[lb]; up=ub; down=lb;
48 37 33 92 86 57 while (down < up) {
while (x[down] <= a && down < ub) down++;
down up while (x[up] > a) up--;
if (down < up) interchange x[down] with x[up]
33 37 }
48 92 86 57
x[lb] = x[up]; x[up] = a; *pj = up;
}
x[lb] = x[up]=33; x[up] = a = 48; *pj = 4;
quick(x,2,3);
quick(x,5,7);
Sorting
Quick Sort (Example)
a partition(x,lb,ub,pj) {
33 37 lb=2, ub=3, a=x[lb]=33;
a=x[lb]; up=ub; down=lb;
x[lb] = x[up]=33; while (down < up) {
down up x[up] = a = 33; while (x[down] <= a && down < ub) down++;
*pj = 2; while (x[up] > a) up--;
quick(x,2,1); not if (down < up) interchange x[down] with x[up]
quick(x,3,3); 🡪 processed }
x[lb] = x[up]; x[up] = a; *pj = up;
}
a quick(x,lb,ub) {
92 86 57 lb=5, ub=7, a=x[lb]=92; if (lb >= ub) return;
partition(x,lb,ub,j);
down up x[lb] = x[up]=57; quick(x,lb,j-1);
x[up] = a = 92; quick(x,j+1,ub);
*pj = 7; }
57 86 92 quick(x,5,6);
quick(x,8,7);🡪not processed
Sorting
Quick Sort (Example)
partition(x,lb,ub,pj) {
a x[lb] = x[up]=57;
57 86 a=x[lb]; up=ub; down=lb;
x[up] = a = 57; while (down < up) {
down up *pj = 5; while (x[down] <= a && down < ub) down++;
quick(x,5,4); not while (x[up] > a) up--;
🡪
quick(x,6,6); processed if (down < up) interchange x[down] with x[up]
}
57 86 x[lb] = x[up]; x[up] = a; *pj = up;
}
Sorting
Selection Sort
Sorting
Straight Selection Sort
(algorithm)
n=6;
selection (x,n) { large=48; index=0; 48 37 57 92 86 33
for (i=n-1; i>0; i--) { large=57; index=2;
large=x[0]; index=0; large=92; index=3;
for (j=1; j<i; j++) { j i
if (x[j]>large) {
large=x[j]; index=j; } large=48; index=0;
large=57; index=2; 48 37 57 33 86 92
}
if (x[j]>large) { large=86; index=4;
x[index]=x[i]; x[i]=large;} j i
} large=48; index=0;
} 48 37 57 33 86 92
large=57; index=2;
j i
large=48; index=0;
48 37 33 57 86 92
j i
large=33; index=0;
33 37 48 57 86 92
Sorting
j i
Insertion Sort
Sorting
Simple Insertion Sort (example)
insertSort(x,n) {
for (i=1; i< n; i++) {
y=x[i];
// move up 1 position all elements greater than y
for (j=i-1; j>=0 && y<x[j]; j--)
x[j+1]=x[j];
// insert y at proper position
x[j+1]=y; 25 57 48 37 12 92 86 33 Do nothing
}
j i i=1; Y=57; j=0
}
25 57
48 48 37 12 92 86 33
25 48
37 57 37 12 92 86 33
12
25 37 48 57 12 92 86 33
i=4; Y=12; j=3
j i insertSort(x,n) {
i=5; Y=92; j=4 for (i=1; i< n; i++) {
y=x[i];
12 25 37 48 57 92 86 33 // move up 1 position all elements
Do nothing greater than y
j i for (j=i-1; j>=0 && y<x[j]; j--)
i=6; Y=86; j=5 x[j+1]=x[j];
12 25 37 48 57 92
86 86 33 // insert y at proper position
x[j+1]=y;
j i }
}
i=7; Y=33; j=6
12 25 33
37 48 57 86 92 33
j i
Sorting
Shell Sort
Sorting
Shell Sort (example)
shellSort(x,n) {
for (incr=0; incr< numinc; incr++) {
// span is the size of the incrmnts
span=incrmnts[incr];
for (j=span; j< n; j++){
y=x[j];
for(k=j-span; k>=0 && y<x[k]; k-=span)
x[k+span]=x[k];
x[k+span]=y; Original records: 25 57 48 37 12 92 86 33
incrmnts=(5,3,1)
}
} Pass 1, span=5: 25 57 48 37 12 92 86 33
} j=5; Y=92; k=0
k j
Pass 1, span=5: 25 57 48 37 12 92 86 33
j=6; Y=86; k=1
k j
Pass 1, span=5: 25 57 33
48 37 12 92 86 48
33
j=7; Y=33; k=2 Sorting
k j
Shell Sort (example)
Pass 2, span=3: 25 12
57 33 37 12 92
57 86 48
j=3; Y=37; k=0
j=4; Y=12; k=1
k j
Pass 2, span=3: 25 12 33 37 57 92
48 86 48
57
j=5; Y=92; k=2
j=6; Y=86; k=3
j=6; Y=86; k=0 k j
j=7; Y=48; k=4
j=7; Y=48; k=1
Pass 3, span=1: 25
12 25
12 33 37 48 92 86 57
j=1; Y=12; k=0
k j
Pass 3, span=1: 12 25 33 37 48 92
86 86
92 57
j=2; Y=33; k=1
j=3; Y=37; k=2
j=6; Y=86; k=5
k j
Sorting
Shell Sort (example)
Pass 3, span=1: 12 25 33 37 48 57
86 92
86 92
57
j=7; Y=57; k=6
j=7; Y=57; k=5
j=7; Y=57; k=4
k j
Sorting
Merge and Radix Sort
■ Merge sort:
■ combining two or more sorted element
■ Radix sort:
■ based on the value of actual digits in
positional representations of number being
sorted
Sorting
Merge Sort
Original: 25 57 48 37 12 92 86 33
Pass 1: 25 57 37 48 12 92 33 86
Pass 2: 25 37 48 57 12 33 86 92
Pass 3: 12 25 33 37 48 57 86 92
Sorting
Assignment (30 September 2020)
Sorting