Вы находитесь на странице: 1из 43

SEARCHING AND SORTING

SEARCHING
Bubble sort Selection Sort Insertion Sort Quick Sort Radix Sort Heap Sort

SORTING

Rearrange element of list, So they are in ascending or descending order.

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

BUBBLE SORT (EXAMPLE)


example: 1st step: 25 25 25 25 25 25 25 25 57 57 57 48 48 48 48 48 48 48 48 37 37 37 37 57 12 12 12 12 12 12 12 12 57 12 57 57 57 92 92 92 92 92 92 86 92 86 86 86 86 33 33 33 33 33 33 33 92 33 x0 < x1 no interchange

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

BUBBLE SORT (EXAMPLE)


Original data: After 1st pass: 25 25 57 48 37 12 25 25 25 25 48 37 12 37 37 33 33 33 37 12 48 48 33 37 37 37 12 57 57 33 48 48 48 48 92 86 33 57 57 57 57 57 86 33 86 86 86 86 86 86 33 92 92 92 92 92 92 92

After 2nd pass: 25 After 3rd pass: After 4th pass: After 5th pass: After 6th pass: After 7th pass: 25 12 12 12 12

Bubble sort (algorithm)

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

STRAIGHT SELECTION SORT (ALGORITHM)


n=6; large=48; index=0; 48 large=57; index=2; large=92; index=3; large=48; index=0; 48 large=57; index=2; large=86; index=4; large=48; index=0; 48 large=57; index=2; 37 j 37 57 33 86 i 57 33 i 33 i 48 57 86 92 57 86 92 86 92 57 92 86 33

i
92

j
37 j

large=48; index=0; 48

37 j

large=33; index=0; 33

37

j i

SELECTION SORT (ALGORITHM)


while(length>0) { int max=0; int i; for(i=1; to length-1) { if(array[max]<array[i]) { max=i; } } exchange array[max] with array[ukuran-1] ukuran--; }

INSERTION SORT

It sorts a set of records by inserting records into an existing sorted file

Sorting

SIMPLE INSERTION SORT (EXAMPLE)

25 j 25

57 i 57 48

48

37

12

92

86

33 Do nothing

i=1; Y=57; j=0 48 37 12 92 86 33

j
25 48 37

i
57 37 12

i=2; Y=48; j=1 92 86 33

i=3; Y=37; j=2

Sorting

SIMPLE INSERTION SORT (EXAMPLE)


12 25 37 48 57 j 12 25 37 48 12 i 57 j 12 25 37 48 57 92 86 33 i=4; Y=12; j=3 i=5; Y=92; j=4 92 86 33 i Do nothing

i=6; Y=86; j=5 92 86 86 33 j i

12

25

33 37

48

57

i=7; Y=33; j=6 86 92 33 j i

INSERTION SORT (ALGORITHM)


int i; int max=1; while(max<ukuran) { int temp=array[max]; for(i=max; to 1) { if(temp<array[i-1]) { array[i]=array[i-1]; }else break; } array[i]=temp; max++; }

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

QUICK SORT (EXAMPLE)


i p 2 i 2 i 2 p 8 7 p 8 j 8 j 7 j 1 j 8 p 2 1 i 7 8 3 7 1 3 j 5 j 8 i 1 3 8 7 5 6 7 5 6 j 4 4 6 4 5 6 4 3 5 6 4 1 3 5 6 4 7 1 3 5 6 4

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

QUICK SORT (ALGORITHM)


Quicksort(A,p,r) { if(p<r) q=partition(A,p,r) Quicksort(A,p,q-1) Quicksort(A,q+1,r) }

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

RADIX SORT (ALGORITHM)

It needs several function


To

representing bit To swap the list Its main function

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

To swap the list


void swap(int[] tukar, int[] array) for(int i=0;i<array.length;i++) { array[i]=tukar[i]; } }

Its main function

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

Make your own code for :


Bubble
Selection Insertion

Quick
radix

HEAP SORT

Is an array object that we can view as a nearl complete binary tree

MAPPING INTO AN ARRAY


16 4 10

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;

MAINTANING THE HEAP PROPERTY


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

BINARY SEARCH (EXAMPLE)


n=13; lo=0; hi=13-1=12; mid=(0+12)/2; key=512; example: 61 87 154 170 275 426 503 512 612 653 765 897 908

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

amid>key hi=mid-1=8; mid=(7+8)/2=7;

lo

mid

hi

61 87 154 170 275 426 503 512 612 653 765 897 908

lo mid hi
amid=key terminate

Searching

RECURSIVE BINARY SEARCH

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

Вам также может понравиться