Sunteți pe pagina 1din 3

Insertion Sort: ------------------Insertion (item [], N) /* item is an array of size N*/ { For (I=1; I<N; I++) { Initialize X by item

[I]. /*finding the proper position of the target element*/ For (J=I-1; J>=0 AND item [J]>X; J--) Update item [J+1] by item [J]; /*inserting the target element into its proper position*/ Update item [J+1] by X; } } Quick sort: --------------Quick (item [], start, end) { /* item is an array of size N and start and end are the starting and ending posi tion of the element respectively*/ Loc=split (item, start, end); If loc>start Quick (item, start, loc-1); If end>loc Quick (item, loc+1, end); } Integer split (item, start, end) { Initialize down and up by start and end respectively. Set pivot is equals to item [start]; While (up>down) { While (item [up]>pivot) decrement up by 1; While (item [down] <=pivot AND down<end) increment down by 1; If up>down then swap item [up] and item [down]; } Set item [start] is equals to item [up]; Update item [up] by pivot; Return up; } Merge sort: --------------Merge (item [], low, high) { /* item is an array of size N and low and high are the starting and ending posit ion of the element respectively. And temp is also an array of size N*/ If (low<high) { Set mid is equals to (low +high)/2; Merge (item, low, mid); Merge (item, mid+1, high); For (I=low, J=mid +1, K=low; I<=mid AND J<=high; K++) If (item [I] <item [J]) Set temp [K] is equals to item [I] and increment I by 1; Else set temp [K] is equals to item [J] increment J by 1; While (I<=mid) Set temp [K] is equals to item [I] increment I and K by 1; While (J<=high) Set temp [K] is equals to item [J] increment J and K by 1;

/*copying element from temporary array to actual array*/ For (K=low; K<=high; K++) Update item [K] by temp [K]; } } Radix sort: ---------------Radix (item [], N) /* item is an array of size N */ { /*Q is an array of Circular Queue of size 10;*/ Initialize array Q; Find out the maximum element among the elements of the input array; Count the digit of the maximum element and store the value into pass; Initialize div by 1; For (I=1; I<=pass; I++) { /*inserting the elements into queue*/ For (J=0; J<N; J++) { Set remainder is equals to item [J] %( div*10); Update remainder by (remainder/div); Insert (&Q [remainder], item [J]); } Update div by (div*10); /*deleting the elements from the queue and place them into the array*/ For (J=0, index=0; J<10; J++) While (! Isempty (&Q [J])) item [index++] = Delete (&Q [J]); } } Shell sort: --------------Shell (item [], N) /* item is an array of size N */ { Initialize gap by N/2; While (gap) { For (I=gap; I<N; I++) { Initialize X by item [I]. /*finding the proper position of the target element*/ For (J=I- gap; J>=0 AND item [J]>X; J-=gap) Update item [J+ gap] by item [J]; /*inserting the target element into its proper position*/ Update item [J+ gap] by X; } Update gap by gap/2; } } Heap sort: -------------Heap (item [], N) /* item is an array of size N */ { Build_heap (item, N); Heapify (item, N); } Build_heap (item [], N) { For (I=1; I<N; I++) { Initialize child by I;

Do { Set Parent is equals to (child-1)/2; If item [parent] <item [child] then swap item [parent and item [child]; Update child by parent; } While (parent not equals to 0); } } Heapify (item [], N) { For (I=N-1; I>0; I--) { Swap item [0] and item [I]; Build_heap (item, I); } }

S-ar putea să vă placă și