Академический Документы
Профессиональный Документы
Культура Документы
SEARCHING
Bubble sort Selection Sort Insertion Sort Quick Sort Radix Sort Heap Sort
SORTING
BUBBLE SORT
Easy to understand, but But least efficient
Its
pass throught the element sequential. Each pass consist of comparing each element with its successor (xi with xi+1) and interchange them if they are not in proper order.
Sorting
2nd step:
3rd step: 4th step: 5th step: 6th step: 7th step:
x1 > x2 interchange
x2 > x3 interchange x3 > x4 interchange x4 < x5 no interchange x5 > x6 interchange x6 > x7 interchange
57
37 37 37 37
86
86 86 86 92 92 33
Sorting
After 2nd pass: 25 After 3rd pass: After 4th pass: After 5th pass: After 6th pass: After 7th pass: 25 12 12 12 12
while(length>0) { for(int i=1; to length-1) { if(array[i]<array[i-1]) { exchange array[i] with array[i-1] } } length--; }
SELECTION SORT
Its named also as push-down sort The Largest of remaining elements is placed into its proper position
Sorting
i
92
j
37 j
large=48; index=0; 48
37 j
large=33; index=0; 33
37
j i
INSERTION SORT
Sorting
25 j 25
57 i 57 48
48
37
12
92
86
33 Do nothing
j
25 48 37
i
57 37 12
Sorting
12
25
33 37
48
57
QUICK SORT
Quick Sort Is divide and conquer algorithm.
Divide : means re arrange the array into two part into two (possibly empty) subarrays A[p q-1] and A[q+1 .. r] such that each element of A[p q-1] less than or equal to A[q], Conquer : sort both of subarrays A[p q-1] and A[q+1 .. r] by recursive
i
2
p
2 p 2 1 3
p 2 p 2 p 1 3 1 3
i 8 i 8 i 7 5 6 4 7 5 6 4
q
4 7 5 6 8
Do the same things for subarrays A[p q-1] and A[q+1 .. r] recursively
Partition(A,p,r) x=A[r] i=p-1 for(j=p; to r-1) if(A[j])<=x i=i+1; Exchange A[i] with A[j] Exchange A[i] with A[j] return i+1;
RADIX SORT
Sort with respect to the least significant digit. Sort with respect to the least two significant digit. so on
To representing bits
int cekbit(int array , int bit) for(int i=1;i<bit;i++) { array/=2; } return array%2;
RADIX SORT
5 2
(EXAMPLE)
0101
0010
0111
0011
1001
1000
0100
0001
0010
1000
0100
0101
0111
0011
1001
0001
1000
0100
0101
1001
0001
0010
0111
0011
1000 0001
1001 0010
0001 0011
0010 0100
0011 0101
0100 0111
0101 1000
0111 1001
void radix(int[] array, int bit) { int s; int[] tukar=new int[array.length]; for(int i=1;i<=bit;i++) { s=0; for(int k=0;k<=1;k++) { for(int j=0;j<array.length;j++) { if(cekbit(array[j], i)==k) tukar[s]=array[j]; s++; } } swap(tukar, array); } }
HEAP SORT
SEARCHING
ASSIGNMENT
Quick
radix
HEAP SORT
14
2
1 2
8
3 4
1
5 6 7 8 9 10
16 4 10 14 7
Notice
left(i) return 2i; Right return (2i)+1;
16
i
14
8 4
4 7
5 6
10 9 3
7
9 10
Its function max-heapify(A,i) l=left(i) r=right(i) if i<A.heap-size and A[l]>A[i] max=l else max=i; if max<A.heap.size and A[r]>A[max] max=r; if max!=i exchange A[i] with A[max]; max-heapify(A,max)
BUILDING A HEAP
Build-max-heap A-heap-size=A.length; for i=[A.length/2] down to 1 max-heapify(A,i)
1
4
2 3
1
16
5 6
3
9 10
7
2 14
8
9 10
4
2
3
1 16
5 6
3 9 10
7
14
9 10
4
2 3
1 16
5 6
3 9 10
7
14
8
9 10
4
2 3
1 16
5 6
10 3 9
7
14
8
9 10
4 16 14
8 4 2 3
10 7
5 6
9 10
MAIN FUNCTION
heapsort(A) build-max-heap(A) for i=A.length down to 2 exchange A[1] with A[i] A.heap-size=A.heap-size-1 Max-heapify(A,1)
SEARCHING
Binary search Array search
Binary (array, cari) low=0; high=array.length-1; mid=(low+high)/2; while((low<high)&&(!ketemu)) { value=array[mid]; if(value==cari) { ketemu=true; lokasi=mid; break; }else { if(cari<array[mid]) high=mid; else low=mid+1; } mid=(low+high)/2; }
Searching
BINARY SEARCH
The algorithm
lo=0; hi=n-1 while (lo hi) { mid=(lo+hi)/2; if (key==k(mid)) return mid; if (key<k(mid)) hi=mid-1; else lo=mid+1; } return -1;
Searching
lo
amid<key lo=mid+1=7; mid=(7+12)/2=9;
mid
hi
61 87 154 170 275 426 503 512 612 653 765 897 908
lo
mid
hi
61 87 154 170 275 426 503 512 612 653 765 897 908
lo mid hi
amid=key terminate
Searching
The algorithm
binSearch(lo,hi); //lo=0; hi=n-1 while (lo hi) { mid=(lo+hi)/2; if (key==k(mid)) return mid; if (key<k(mid)) binSearch(lo, mid-1); else binSearch(mid+1,hi); } return -1;
ARRAY SEARCH
If the value is smaller that array[index], the searching process will stop array(A,find) while((!found)&&(find<=A[index])&&(index<high) { value=A[index] I f(find==value) found=true; i=index; break index++; }
; key=50; example: 61 87 154 170 275 426 503 512 612 653 765 897 908