Vous êtes sur la page 1sur 23

Lecture-8(sorting)

Selection sort
It works by selecting the smallest (or largest, if you want to sort from big to small) element of the array and placing it at the head of the array. Then the process is repeated for the remainder of the array; the next largest element is selected and put into the next slot, and so on down the line. In the first iteration, 1st element is compared against all the other elements (from array index 1 to array index n). In the second iteration 2nd element is compared with the elements from array index 2 to n. This process is repeated n-1 times.

#include <stdio.h> void main( ) { int arr[5] = { 25, 17, 31, 13, 2 } ; int i, j, temp ; for ( i = 0 ; i <= 3 ; i++ ) { for ( j = i + 1 ; j <= 4 ; j++ ) { if ( arr[i] > arr[j] ) { temp = arr[i] ; arr[i] = arr[j] ; arr[j] = temp ; } } }
printf ( "\n\nArray after sorting:\n") ; for ( i = 0 ; i <= 4 ; i++ ) printf ( "%d\t", arr[i] ) ; }

Selection Sort

For example, consider the following array, shown with array elements in sequence separated by commas: The leftmost element is at index zero, and the rightmost element is at the highest array index, in our case, 4 (the effective size of our array is 5). The largest element in this effective array (index 0-4) is at index 2. We have shown the largest element and the one at the highest index in bold. We then swap the element at index 2 with that at index 4. The result is: We reduce the effective size of the array to 4, making the highest index in the effective array now 3. The largest element in this effective array (index 0-3) is at index 1, so we swap elements at index 1 and 3 (in bold): The next two steps give us:

Example:insertion sort
ENTER NO 1 7 2 5 3 pass 2 : 1 7 2 5 3 pass 3 : 1 2 7 5 3 pass 4 : 1 2 5 7 3 pass 5 : 1 2 3 5 7 SORTED ARRAY. 12357

ALGORITHM:insertion sort
INSERTION_SORT (A) 1. FOR j 2 TO length[A] 2. DO key A[j] 3. {Put A[j] into the sorted sequence A[1 . . j 1]} 4. ij1 5. WHILE i > 0 and A[i] > key 6. DO A[i +1] A[i] 7. ii1 8. A[i + 1] key

#include <stdio.h> int main() { int n, array[1000], c, d, t; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for (c = 0; c < n; c++) { scanf("%d", &array[c]); } for (c = 1 ; c <= n - 1; c++) { d = c; while ( d > 0 && array[d] < array[d-1]) { t = array[d]; array[d] = array[d-1]; array[d-1] = t; d--; } } printf("Sorted list in ascending order:\n"); for (c = 0; c <= n - 1; c++) { printf("%d\n", array[c]); } return 0;

Insertion sort

Quick Sort
Divide and conquer idea: Divide problem into two smaller sorting problems. Divide: Select a splitting element (pivot) Rearrange the array (sequence/list)

QuickSort 9

Quick Sort
Result: All elements to the left of pivot are smaller or equal than pivot, and All elements to the right of pivot are greater or equal than pivot pivot in correct place in sorted array/list

Need: Clever split procedure (Hoare)

QuickSort 10

Quick Sort
Divide: Partition into subarrays (sub-lists) Conquer: Recursively sort 2 subarrays Combine: Trivial

QuickSort 11

Quicksort Code
P: first element r: last element Quicksort(A, p, r) { if (p < r) { q = Partition(A, p, r) Quicksort(A, p , q-1) Quicksort(A, q+1 , r) } } Initial call is Quicksort(A, 1, n), where n in the length of A

Partition
Clearly, all the action takes place in the partition() function
Rearranges the subarray in place End result:
Two subarrays All values in first subarray all values in second

Returns the index of the pivot element separating the two subarrays

Partition Code
Partition(A, p, r) { x = A[r] // x is pivot i = p - 1 for j = p to r 1 { do if A[j] <= x then { i = i + 1 exchange A[i] A[j] } } exchange A[i+1] A[r] return i+1 }

partition() runs in O(n) time

Divide-and-Conquer
Divide the problem into a number of sub-problems
Similar sub-problems of smaller size

Conquer the sub-problems


Solve the sub-problems recursively Sub-problem size small enough solve the problems in straightforward manner

Combine the solutions of the sub-problems


Obtain the solution for the original problem
15

To sort an array A[p . . r]: Divide

Merge Sort Approach

Divide the n-element sequence to be sorted into two subsequences of n/2 elements each

Conquer
Sort the subsequences recursively using merge sort When the size of the sequences is 1 there is nothing more to do

Combine
Merge the two sorted subsequences
16

Merge Sort
p

q
2 3 4 5 6 7 8

r 1 3 2 6

Alg.: MERGE-SORT(A, p, r)
if p < r then q (p + r)/2 MERGE-SORT(A, p, q) MERGE-SORT(A, q + 1, r) MERGE(A, p, q, r)

5 2

Check for base case Divide Conquer Conquer Combine

Initial call: MERGE-SORT(A, 1, n)


17

Example n Power of 2
1 2 3 4 5 6 7 8

Divide
1 2

5 2

q=4

5 2

1 3

5 2

1 3

18

Example n Power of 2
1 2 3 4 5 6 7 8

Conquer and Merge

1 2

2 4

1 2

2 5

1 3

19

Example n Not a Power of 2


1 2 3 4 5 6 7 8 9 10 11

Divide
1 2

4
3

7
4

2
5

6
6

7
7

3
8

5
9

2
10

6
11

q=6

q=3

4
1 2

7
3

6
4

1
5

4
6 7

7
8

3
9

2
10

6
11

q=9

4
1 2

2
3 4

6
5

4
6 7

7
8

5
9 10

6
11

4
1

7
2

2
4

1
5

4
7

3
8

20

Example n Not a Power of 2


1 2 3 4 5 6 7 8 9 10 11

Conquer and Merge


1

1
1 2 3

2
4

2
5

3
6

5
7

6
8

6
9

7
10

7
11

1
2

2
3

4
4

6
5

7
6 7

2
8

3
9

6
10

7
11

2
1 2

7
3 4

1
5

6
6 7

3
8

7
9 10

6
11

4
1

7
2

2
4

6
5

4
7

7
8

21

Merging
p
1 2 3 4

q
5 6 7 8

r
1 2 3 6

2 4

Input: Array A and indices p, q, r such that pq<r


Subarrays A[p . . q] and A[q + 1 . . r] are sorted

Output: One single sorted subarray A[p . . r]

22

Alg.: MERGE(A, p, q, r)

Merge - Pseudocode p
1 2 3 4

q
5 6 7 8

r
1 2 3 6

1. Compute n1 and n2 2. Copy the first n1 elements into n1 . . n1 + 1] and the next n2 elements into R[1 . . n2 + 1] p 3. L[n1 + 1] ; R[n2 + 1] 2 4 5 L 4. i 1; j 1 q+1 5. for k p to r R 1 2 3 6. do if L[ i ] R[ j ] 7. then A[k] L[ i ] 8. i i + 1 9. else A[k] R[ j ] 10. jj+1
23

2 4

L[1 n2
q

7
r

Vous aimerez peut-être aussi