data structure lecture 9 sorting[1]

Upload: sweetmaina

Post on 03-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    1/23

    Lecture-8(sorting)

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    2/23

    Selection sort

    It works by selecting the smallest (or largest, if you

    want to sort from big to small) element of the arrayand 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 intothe 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 arrayindex n). In the second iteration 2nd element iscompared with the elements from array index 2 to n.This process is repeated n-1 times.

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    3/23

    Selection Sort#include void main( )

    {

    int arr[5] = { 25, 17, 31, 13, 2 } ;

    int i, j, temp ;

    for ( i = 0 ; i

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    4/23

    For example, consider the following array, shown with arrayelements in sequence separated by commas:

    The leftmost element is at index zero, and the rightmost element isat the highest array index, in our case, 4 (the effective size of ourarray 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 thehighest index in bold. We then swap the element at index 2 withthat at index 4. The result is:

    We reduce the effective size of the array to 4, making the highestindex in the effective array now 3. The largest element in thiseffective 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:

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    5/23

    Example:insertion sort

    ENTER NO17253

    pass 2 : 1 7 2 5 3pass 3 : 1 2 7 5 3pass 4 : 1 2 5 7 3

    pass 5 : 1 2 3 5 7

    SORTED ARRAY.1 2 3 5 7

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    6/23

    ALGORITHM:insertion sort

    INSERTION_SORT (A)

    1. FORj 2TO length[A]2. DO key A[j]

    3. {PutA[j] into the sortedsequenceA[1 . .j 1]}4. ij 15. WHILEi> 0 andA[i] > key

    6. DOA[i+1] A[i]7. ii 18. A[i+ 1] key

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    7/23

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    8/23

    Insertion sort#include

    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 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

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    9/23

    QuickSort 9

    Quick Sort

    Divide and conquer idea: Divide problem into two

    smaller sorting problems.

    Divide: Select a splitting element (pivot)

    Rearrange the array (sequence/list)

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    10/23

    QuickSort 10

    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 orequal than pivot

    pivot in correct place in sorted array/list

    Need: Clever split procedure (Hoare)

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    11/23

    QuickSort 11

    Quick Sort

    Divide: Partition into subarrays (sub-lists)

    Conquer: Recursively sort 2 subarrays

    Combine: Trivial

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    12/23

    Quicksort CodeP: 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 ofA

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    13/23

    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 indexof the pivot elementseparating the two subarrays

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    14/23

    Partition CodePartition(A, p, r)

    {

    x = A[r] // x is pivoti = p - 1

    for j = p to r 1

    {

    do if A[j]

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    15/23

    15

    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

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    16/23

    16

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

    Divide the n-element sequence to be sorted into twosubsequences ofn/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

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    17/23

    17

    Merge Sort

    Alg.: MERGE-SORT(A, p, r)

    ifp < r Check for base case

    thenq

    (p + r)/2

    Divide

    MERGE-SORT(A, p, q) Conquer

    MERGE-SORT(A, q + 1, r) Conquer

    MERGE(A, p, q, r) Combine

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

    1 2 3 4 5 6 7 8

    62317425

    p rq

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    18/23

    18

    Examplen Power of 2

    1 2 3 4 5 6 7 8

    q = 462317425

    1 2 3 4

    7425

    5 6 7 8

    6231

    1 2

    25

    3 4

    74

    5 6

    31

    7 8

    62

    1

    5

    2

    2

    3

    4

    4

    7 1

    6

    3

    7

    2

    8

    6

    5

    Divide

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    19/23

    19

    Examplen Power of 2

    1

    5

    2

    2

    3

    4

    4

    7 1

    6

    3

    7

    2

    8

    6

    5

    1 2 3 4 5 6 7 8

    76543221

    1 2 3 4

    7542

    5 6 7 8

    6321

    1 2

    52

    3 4

    74

    5 6

    31

    7 8

    62

    Conquer

    and

    Merge

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    20/23

    20

    Examplen Not a Power of 2

    62537416274

    1 2 3 4 5 6 7 8 9 10 11

    q = 6

    416274

    1 2 3 4 5 6

    62537

    7 8 9 10 11

    q = 9q = 3

    274

    1 2 3

    416

    4 5 6

    537

    7 8 9

    62

    10 11

    74

    1 2

    2

    3

    16

    4 5

    4

    6

    37

    7 8

    5

    9

    2

    10

    6

    11

    4

    1

    7

    2

    6

    4

    1

    5

    7

    7

    3

    8

    Divide

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    21/23

    21

    Examplen Not a Power of 2

    77665443221

    1 2 3 4 5 6 7 8 9 10 11

    764421

    1 2 3 4 5 6

    76532

    7 8 9 10 11

    742

    1 2 3

    641

    4 5 6

    753

    7 8 9

    62

    10 11

    2

    3

    4

    6

    5

    9

    2

    10

    6

    11

    4

    1

    7

    2

    6

    4

    1

    5

    7

    7

    3

    8

    74

    1 2

    61

    4 5

    73

    7 8

    Conquerand

    Merge

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    22/23

    22

    Merging

    Input: Array Aand indices p, q, rsuch that

    p q < r

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

    Output: One single sorted subarrayA[p . . r]

    1 2 3 4 5 6 7 8

    63217542

    p rq

  • 7/28/2019 Data Structure Lecture 9 Sorting[1]

    23/23

    23

    Merge - Pseudocode

    Alg.: MERGE(A, p, q, r)1. Compute n1and n2

    2. Copy the first n1 elements into L[1. . n1 + 1] and the next n2 elements into R[1 . . n2 + 1]

    3. L[n1 + 1]; R[n2 + 1] 4. i 1; j 1

    5. for k pto r

    6. do ifL[ i ] R[ j ]

    7. then A[k] L[ i ]

    8. ii + 1

    9. else A[k] R[ j ]

    10. j

    j + 1

    p q

    7542

    6321

    rq + 1

    L

    R

    1 2 3 4 5 6 7 8

    63217542

    p rq

    n1 n2