SlideShare une entreprise Scribd logo
1  sur  60
Télécharger pour lire hors ligne
Searching Elements in an Array:
               Linear and Binary Search




Spring Semester 2007   Programming and Data Structure   1
Searching

• Check if a given element (called key) occurs
  in the array.
    – Example: array of student records; rollno can be
      the key.
• Two methods to be discussed:
    – If the array elements are unsorted.
          • Linear search
    – If the array elements are sorted.
          • Binary search




Spring Semester 2007   Programming and Data Structure   2
Linear Search




Spring Semester 2007   Programming and Data Structure   3
Basic Concept

• Basic idea:
    – Start at the beginning of the array.
    – Inspect elements one by one to see if it matches the key.
• Time complexity:
    – A measure of how long an algorithm takes to run.
    – If there are n elements in the array:
          • Best case:
             match found in first element (1 search operation)
          • Worst case:
            no match found, or match found in the last element
            (n search operations)
          • Average case:
               (n + 1) / 2 search operations

Spring Semester 2007    Programming and Data Structure           4
Contd.
/* The function returns the array index where the
   match is found. It returns -1 if there is no
   match.    */

int      linear_search (int a[], int size, int key)
{
      int pos = 0;
      while ((pos < size) && (a[pos] != key))
         pos++;
      if (pos < size)
         return pos; /* Return the position of match */
      return -1;      /* No match found */
}



    Spring Semester 2007   Programming and Data Structure   5
Contd.

       int x[]= {12, -3, 78, 67, 6, 50, 19, 10};


• Trace the following calls :
     search (x, 8, 6) ;                       Returns 4
     search (x, 8, 5) ;



              Returns -1



Spring Semester 2007   Programming and Data Structure     6
Binary Search




Spring Semester 2007   Programming and Data Structure   7
Basic Concept

• Binary search works if the array is sorted.
    – Look for the target in the middle.
    – If you don’t find it, you can ignore half of the
      array, and repeat the process with the other
      half.
• In every step, we reduce the number of
  elements to search in by half.




Spring Semester 2007   Programming and Data Structure    8
The Basic Strategy

• What we want?
  – Find split between values larger and smaller than key:
                0                                            n-1
       x:                <=key                    >key
                L                                            R

  – Situation while searching:
       • Initially L and R contains the indices of first and last elements.
  – Look at the element at index [(L+R)/2].
       • Move L or R to the middle depending on the outcome of test.



  Spring Semester 2007      Programming and Data Structure          9
Contd.
/* If key appears in x[0..size-1], return its location, pos
    such that x[pos]==key. If not found, return -1 */

int bin_search (int x[], int size, int key)
{
  int L, R, mid;
  _________________;
  while ( ____________ )
  {
       __________________;
  }
  _________________ ;
}




Spring Semester 2007   Programming and Data Structure         10
The basic search iteration

int bin_search (int x[], int size, int key)
{
  int L, R, mid;
  _________________;
  while ( ____________ )
  {
     mid = (L + R) / 2;
     if (x[mid] <= key)
        L = mid;
     else R = mid;
  }
  _________________ ;
}



Spring Semester 2007   Programming and Data Structure   11
Loop termination

int bin_search (int x[], int size, int key)
{
  int L, R, mid;
  _________________;
  while ( L+1 != R )
  {
     mid = (L + R) / 2;
     if (x[mid] <= key)
        L = mid;
     else R = mid;
  }
  _________________ ;
}



Spring Semester 2007    Programming and Data Structure   12
Return result

int bin_search (int x[], int size, int key)
{
  int L, R, mid;
  _________________;
  while ( L+1 != R )
  {
     mid = (L + R) / 2;
     if (x[mid] <= key)
         L = mid;
     else R = mid;
  }
  if (L >= 0 && x[L] == key) return L;
  else return -1;
}

Spring Semester 2007   Programming and Data Structure   13
Initialization

int bin_search (int x[], int size, int key)
{
  int L, R, mid;
  L = -1;   R = size;
  while ( L+1 != R )
  {
     mid = (L + R) / 2;
     if (x[mid] <= key)
        L = mid;
     else R = mid;
  }
  if (L >= 0 && x[L] == key) return L;
  else return -1;
}

Spring Semester 2007   Programming and Data Structure   14
Binary Search Examples

                       Sorted array

    -17 -5 3 6 12 21 45 63 50


                                                     L= –1; R= 9;   x[4]=12;
 Trace :
                                                     L= –1; R=4;    x[1]= –5;
           binsearch (x, 9, 3);                      L= 1; R=4;     x[2]=3;
                                                     L=2; R=4;      x[3]=6;
           binsearch (x, 9, 145);                    L=2; R=3;      return L;

           binsearch (x, 9, 45);
We may modify the algorithm by checking equality with x[mid].


Spring Semester 2007       Programming and Data Structure                  15
Is it worth the trouble ?

• Suppose that the array x has 1000 elements.
• Ordinary search
– If key is a member of x, it would require 500 comparisons
   on the average.
• Binary search
    – after 1st compare, left with 500 elements.
    – after 2nd compare, left with 250 elements.
    – After at most 10 steps, you are done.




Spring Semester 2007   Programming and Data Structure         16
Time Complexity

• If there are n elements in the array.
    – Number of searches required:
        log2n
                                                         2k= n, where k is the
• For n = 64 (say).                                        number of steps.
    –   Initially, list size = 64.
    –   After first compare, list size = 32.
    –   After second compare, list size = 16.
                                                                log264 = 6
    –   After third compare, list size = 8.
                                                                log21024 = 10
    –   …….
    –   After sixth compare, list size = 1.

Spring Semester 2007    Programming and Data Structure                    17
Sorting




Spring Semester 2007   Programming and Data Structure   18
The Basic Problem
• Given an array
                x[0], x[1], ... , x[size-1]
   reorder entries so that
                x[0] <= x[1] <= . . . <= x[size-1]

          • List is in non-decreasing order.


• We can also sort a list of elements in non-
  increasing order.


Spring Semester 2007    Programming and Data Structure   19
Example

• Original list:
        10, 30, 20, 80, 70, 10, 60, 40, 70


• Sorted in non-decreasing order:
        10, 10, 20, 30, 40, 60, 70, 70, 80


• Sorted in non-increasing order:
        80, 70, 70, 60, 40, 30, 20, 10, 10



Spring Semester 2007   Programming and Data Structure   20
Sorting Problem

• What we want ?
     – Data sorted in order
                                                          size-1
     0
x:                       Unsorted list




                            Sorted list



 Spring Semester 2007    Programming and Data Structure            21
Selection Sort




Spring Semester 2007   Programming and Data Structure   22
How it works?

• General situation :
          0                          k                   size-1
 x:      smallest elements, sorted   remainder, unsorted


• Step :
   • Find smallest element, mval, in x[k..size-1]
   • Swap smallest element with x[k], then
     increase k.
              0                          k     mval       size-1



                                             swap
Spring Semester 2007    Programming and Data Structure             23
Subproblem

/* Yield index of smallest element in x[k..size-1];*/

int min_loc (int x[], int k, int size)
{
    int j, pos;

       pos = k;
       for (j=k+1; j<size; j++)
           if (x[j] < x[pos])
               pos = j;
       return pos;
}




    Spring Semester 2007   Programming and Data Structure   24
The main sorting function
/* Sort x[0..size-1] in non-decreasing order */

int sel_sort (int x[], int size)
{ int k, m;

    for (k=0; k<size-1; k++)
    {
       m = min_loc (x, k, size);
       temp = a[k];
          a[k] = a[m];           SWAP
          a[m] = temp;
    }
}


Spring Semester 2007   Programming and Data Structure   25
int main()
{
  int x[ ]={-45,89,-65,87,0,3,-23,19,56,21,76,-50};
  int i;
  for(i=0;i<12;i++)
      printf("%d ",x[i]);
  printf("n");
                       -45 89 -65 87 0 3 -23 19 56 21 76 -50
  sel_sort(x,12);      -65 -50 -45 -23 0 3 19 21 56 76 87 89
  for(i=0;i<12;i++)
      printf("%d ",x[i]);
  printf("n");
}


Spring Semester 2007   Programming and Data Structure   26
Example



x: 3 12 -5 6 142 21 -17 45
                                           x: -17 -5 3 6 12 21 142 45
x: -17 12 -5 6 142 21 3 45
                                              x: -17 -5 3 6 12 21 142 45
x: -17 -5 12 6 142 21 3 45
                                              x: -17 -5 3 6 12 21 45 142
x: -17 -5 3 6 142 21 12 45

x: -17 -5 3 6 142 21 12 45

  Spring Semester 2007   Programming and Data Structure           27
Analysis

• How many steps are needed to sort n
  items ?
    – Total number of steps proportional to n2.
    – No. of comparisons?
           (n-1)+(n-2)+……+1= n(n-1)/2



                                  Of the order of n2


    – Worst Case? Best Case? Average Case?

Spring Semester 2007   Programming and Data Structure   28
Insertion Sort




Spring Semester 2007   Programming and Data Structure   29
How it works?

• General situation :
           0                                i                    size-1
 x:                    sorted                   remainder, unsorted

                                            i                             Compare and
                                                                          shift till x[i] is
                                                                          larger.
                                            i


       0                 j                                       size-1




Spring Semester 2007            Programming and Data Structure                       30
Insertion Sort


     void insert_sort (int list[], int size)
     {
       int i,j,item;

         for (i=1; i<size; i++)
            {
              item = list[i] ;
              for (j=i-1; (j>=0)&& (list[j] > i); j--)
                    list[j+1] = list[j];
              list[j+1] = item ;
         }
     }



Spring Semester 2007   Programming and Data Structure   31
int main()
{
  int x[ ]={-45,89,-65,87,0,3,-23,19,56,21,76,-50};
  int i;
  for(i=0;i<12;i++)
      printf("%d ",x[i]);
  printf("n");
                       -45 89 -65 87 0 3 -23 19 56 21 76 -50
  insert_sort(x,12);   -65 -50 -45 -23 0 3 19 21 56 76 87 89
  for(i=0;i<12;i++)
      printf("%d ",x[i]);
  printf("n");
}


Spring Semester 2007   Programming and Data Structure   32
Time Complexity

• Number of comparisons and shifting:
    – Worst case?
            1 + 2 + 3 + …… + (n-1) = n(n-1)/2

    – Best case?
            1 + 1 + …… to (n-1) terms = (n-1)




Spring Semester 2007    Programming and Data Structure   33
Bubble Sort




Spring Semester 2007   Programming and Data Structure   34
How it works?

• The sorting process proceeds in several
  passes.
    – In every pass we go on comparing neighboring
      pairs, and swap them if out of order.
    – In every pass, the largest of the elements under
      considering will bubble to the top (i.e., the right).
               10       5   17   11    -3     12
               5       10   17   11    -3     12
               5       10   17   11    -3     12
               5       10   11   17    -3     12
               5       10   11   -3    17     12
                                                              Largest
               5       10   11   -3    12     17


Spring Semester 2007         Programming and Data Structure             35
• How the passes proceed?
    –   In pass 1, we consider index 0 to n-1.
    –   In pass 2, we consider index 0 to n-2.
    –   In pass 3, we consider index 0 to n-3.
    –   ……
    –   ……
    –   In pass n-1, we consider index 0 to 1.




Spring Semester 2007   Programming and Data Structure   36
Bubble Sort
void swap(int *x, int *y)              void bubble_sort
{                                               (int x[], int n)
  int tmp = *x;                        {
  *x = *y;                               int i,j;
  *y = tmp;
}                                          for (i=n-1; i>0; i--)
                                             for (j=0; j<i; j++)
                                               if (x[j] > x[j+1])
                                                 swap(&x[j],&x[j+1]);
                                       }




   Spring Semester 2007   Programming and Data Structure         37
int main()
{
  int x[ ]={-45,89,-65,87,0,3,-23,19,56,21,76,-50};
  int i;
  for(i=0;i<12;i++)
      printf("%d ",x[i]);
  printf("n");
                       -45 89 -65 87 0 3 -23 19 56 21 76 -50
  bubble_sort(x,12);   -65 -50 -45 -23 0 3 19 21 56 76 87 89
  for(i=0;i<12;i++)
      printf("%d ",x[i]);
  printf("n");
}


Spring Semester 2007   Programming and Data Structure   38
Time Complexity

• Number of comparisons :
    – Worst case?

          1 + 2 + 3 + …… + (n-1) = n(n-1)/2

    – Best case?
       Same




Spring Semester 2007    Programming and Data Structure   39
• How do you make best case with (n-1)
  comparisons only?
    – By maintaining a variable flag, to check if
      there has been any swaps in a given pass.
    – If not, the array is already sorted.




Spring Semester 2007   Programming and Data Structure   40
void bubble_sort
             (int x[], int n)
    {
      int i,j;
      int flag = 0;

        for (i=n-1; i>0; i--)
        {
            for (j=0; j<i; j++)
            if (x[j] > x[j+1])
            {
               swap(&x[j],&x[j+1]);
               flag = 1;
            }
            if (flag == 0) return;
        }

Spring Semester 2007   Programming and Data Structure   41
Some Efficient Sorting Algorithms




Spring Semester 2007   Programming and Data Structure   42
• Two of the most popular sorting algorithms are
  based on divide-and-conquer approach.
    – Quick sort
    – Merge sort
• Basic concept (divide-and-conquer method):
          sort (list)
          {
            if the list has length greater than 1
            {
                Partition the list into lowlist and highlist;
                sort (lowlist);
                sort (highlist);
                combine (lowlist, highlist);
            }
          }

Spring Semester 2007      Programming and Data Structure        43
Quick Sort




Spring Semester 2007   Programming and Data Structure   44
How it works?

• At every step, we select a pivot element in
  the list (usually the first element).
    – We put the pivot element in the final position
      of the sorted list.
    – All the elements less than or equal to the pivot
      element are to the left.
    – All the elements greater than the pivot element
      are to the right.




Spring Semester 2007   Programming and Data Structure   45
Partitioning


          0                                               size-1
 x:
        pivot

              Values smaller                Values greater



               Perform                        Perform
              partitioning                   partitioning



Spring Semester 2007     Programming and Data Structure            46
Example

                       26 33   35    29   19    12    22
                          22   35    29   19    12    33
                          22   12    29   19    35    33   The partitioning
                          22   12    19   29    35    33   process
                       19 22   12    26   29    35    33




               Recursively carry out the partitioning




Spring Semester 2007     Programming and Data Structure                47
void print (int x[], int low, int high)
{
   int i;
    for(i=low; i<=high; i++)
       printf(" %d", x[i]);
    printf("n");
}

void swap (int *a, int *b)
{
  int tmp=*a;
  *a=*b;
  *b=tmp;
}
Spring Semester 2007   Programming and Data Structure   48
void partition (int x[], int low, int high)
{
   int i = low+1, j = high;
   int pivot = x[low];
   if (low >= high) return;
   while (i<j) {
      while ((x[i]<pivot) && (i<high)) i++;
      while ((x[j]>=pivot) && (j>low)) j--;
      if (i<j) swap (&x[i], &x[j]);
   }
   if (j==high) {
      swap (&x[j], &x[low]);
      partition (x, low, high-1);
   }
   else
      if (i==low+1)
         partition (x, low+1, high);
      else {
             swap (&x[j], &x[low]);
             partition (x, low, j-1);
             partition (x, j+1, high);
           }
}
Spring Semester 2007   Programming and Data Structure   49
int main (int argc, char *argv[])
{
   int *x;
   int i=0;
   int num;
    num = argc - 1;
    x = (int *) malloc(num * sizeof(int));

    for (i=0; i<num; i++)
      x[i] = atoi(argv[i+1]);
    partition(x,0,num-1);
    printf(“Sorted list: ");
    print (x,0,num-1);
}



Spring Semester 2007   Programming and Data Structure   50
Trace of Partitioning



       ./a.out 45 -56 78 90 -3 -6 123 0 -3 45 69 68



       45 -56 78 90 -3 -6 123 0 -3 45 69 68

     -6 -56 -3              0    -3    45    123 90 78 45 69 68

    -56     -6 -3           0    -3           68 90 78 45 69 123
                       -3       0 -3            45 68 78 90 69
                            -3     0                      69     78   90


      Sorted list:              -56 -6 -3 -3 0 45 45 68 69 78 90 123


Spring Semester 2007            Programming and Data Structure             51
Time Complexity

• Worst case:
      n2      ==> list is already sorted


• Average case:
      n log2n


• Statistically, quick sort has been found to
  be one of the fastest algorithms.



Spring Semester 2007    Programming and Data Structure   52
• Corollary of quick sort:
    – Given a list of numbers stored in an array,
      determine how many numbers are smaller than
      a given number p?
    – Given a list of integers (negative and non-
      negative), reorder the list so that all negative
      numbers precede the non-negative ones.




Spring Semester 2007   Programming and Data Structure   53
Merge Sort




Spring Semester 2007   Programming and Data Structure   54
Merge Sort

                                    Input Array




                 Part-I                                 Part-II


Part-I                    Part-II                 Part-I            Part-II




            Merge                                           Split
         Sorted Arrays

Spring Semester 2007       Programming and Data Structure                     55
Merging two sorted arrays
           pa                                                   pb


a               Sorted Array                     b              Sorted Array

    0                               m                  0                         n



            c                        Merged sorted array

                0                                                              m+n-1


   Move and copy elements pointed by pa if its value is smaller
 than the element pointed by pb in (m+n-1) operations; otherwise,
                  copy elements pointed by pb .

Spring Semester 2007           Programming and Data Structure                        56
Example

•   Initial array A contains 14 elements:
     – 66, 33, 40, 22, 55, 88, 60, 11, 80, 20, 50, 44, 77, 30
•   Pass 1 :: Merge each pair of elements
     – (33, 66) (22, 40) (55, 88) (11, 60) (20, 80) (44, 50) (30, 70)
•   Pass 2 :: Merge each pair of pairs
     – (22, 33, 40, 66) (11, 55, 60, 88) (20, 44, 50, 80) (30, 77)
•   Pass 3 :: Merge each pair of sorted quadruplets
     – (11, 22, 33, 40, 55, 60, 66, 88) (20, 30, 44, 50, 77, 80)
•   Pass 4 :: Merge the two sorted subarrays to get the final list
     – (11, 20, 22, 30, 33, 40, 44, 50, 55, 60, 66, 77, 80, 88)




Spring Semester 2007   Programming and Data Structure             57
void merge_sort (int *a, int n)
     {
        int i, j, k, m;
        int *b, *c;

          if (n>1) {
             k = n/2;   m = n-k;
             b = (int *) calloc(k,sizeof(int));
             c = (int *) calloc(m,sizeof(int));
             for (i=0; i<k; i++)
                b[i]=a[i];
             for (j=k; j<n; j++)
                c[j-l]=a[j];

               merge_sort (b, k);
               merge_sort (c, m);
               merge (b, c, a, k, m);
               free(b); free(c);
          }
     }


Spring Semester 2007   Programming and Data Structure   58
void merge (int *a, int *b, int *c, int m, int n)
{
   int i, j, k, p;
    i = j = k = 0;
    do {
      if (a[i] < b[j]) {
        c[k]=a[i]; i=i+1;
      }
      else {
        c[k]=b[j]; j=j+1;
      }
      k++;
    } while ((i<m) && (j<n));
    if (i == m) {
       for (p=j; p<n; p++)          { c[k]=b[p]; k++; }
    }
    else {
       for (p=i; p<m; p++)          { c[k]=a[p]; k++; }
    }
}

Spring Semester 2007   Programming and Data Structure     59
main()
{
    int i, num;
    int a[ ] = {-56,23,43,-5,-3,0,123,-35,87,56,75,80};

     for (i=0;i<12;i++)
        printf ("%d ",a[i]);
     printf ("n");

     merge_sort (a, 12);

     printf (“nSorted list:");
     for (i=0;i<12;i++)
        printf (“ %d", a[i]);
     printf ("n");
}


Spring Semester 2007   Programming and Data Structure   60

Contenu connexe

Tendances

Tendances (20)

Linear Search Presentation
Linear Search PresentationLinear Search Presentation
Linear Search Presentation
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sorting
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
 
Quick sort
Quick sortQuick sort
Quick sort
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithn
 
Row major and column major in 2 d
Row major and column major in 2 dRow major and column major in 2 d
Row major and column major in 2 d
 
Arrays
ArraysArrays
Arrays
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 
A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge Sort
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Data structures
Data structuresData structures
Data structures
 
Ppt bubble sort
Ppt bubble sortPpt bubble sort
Ppt bubble sort
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 

En vedette

c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointersSushil Mishra
 
8 elementary sorts-insertion
8 elementary sorts-insertion8 elementary sorts-insertion
8 elementary sorts-insertionirdginfo
 
Bubble sort
Bubble sort Bubble sort
Bubble sort rmsz786
 
Selection sort
Selection sortSelection sort
Selection sortstella D
 
3.4 selection sort
3.4 selection sort3.4 selection sort
3.4 selection sortKrish_ver2
 
Selection sort
Selection sortSelection sort
Selection sortasra khan
 
Bubble sort a best presentation topic
Bubble sort a best presentation topicBubble sort a best presentation topic
Bubble sort a best presentation topicSaddam Hussain
 
Selection sort
Selection sortSelection sort
Selection sortJay Patel
 
Sorting techniques Anil Dutt
Sorting techniques Anil DuttSorting techniques Anil Dutt
Sorting techniques Anil DuttAnil Dutt
 
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...widespreadpromotion
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sortKrish_ver2
 
Linear Search & Binary Search
Linear Search & Binary SearchLinear Search & Binary Search
Linear Search & Binary SearchReem Alattas
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithmNeoClassical
 

En vedette (20)

Sorting
SortingSorting
Sorting
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Sorting
SortingSorting
Sorting
 
c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointers
 
Linear and Bianry search
Linear and Bianry searchLinear and Bianry search
Linear and Bianry search
 
8 elementary sorts-insertion
8 elementary sorts-insertion8 elementary sorts-insertion
8 elementary sorts-insertion
 
Bubble sort
Bubble sort Bubble sort
Bubble sort
 
Selection sort
Selection sortSelection sort
Selection sort
 
The selection sort algorithm
The selection sort algorithmThe selection sort algorithm
The selection sort algorithm
 
Bubble sort algorithm
Bubble sort algorithmBubble sort algorithm
Bubble sort algorithm
 
3.4 selection sort
3.4 selection sort3.4 selection sort
3.4 selection sort
 
Selection sort
Selection sortSelection sort
Selection sort
 
Bubble sort a best presentation topic
Bubble sort a best presentation topicBubble sort a best presentation topic
Bubble sort a best presentation topic
 
Selection sort
Selection sortSelection sort
Selection sort
 
Sorting techniques Anil Dutt
Sorting techniques Anil DuttSorting techniques Anil Dutt
Sorting techniques Anil Dutt
 
Selection sort
Selection sortSelection sort
Selection sort
 
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sort
 
Linear Search & Binary Search
Linear Search & Binary SearchLinear Search & Binary Search
Linear Search & Binary Search
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
 

Similaire à sort search in C

Similaire à sort search in C (20)

L10 sorting-searching
L10 sorting-searchingL10 sorting-searching
L10 sorting-searching
 
Introduction to R.pptx
Introduction to R.pptxIntroduction to R.pptx
Introduction to R.pptx
 
chapter1.pdf ......................................
chapter1.pdf ......................................chapter1.pdf ......................................
chapter1.pdf ......................................
 
Data Structures 7
Data Structures 7Data Structures 7
Data Structures 7
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
Data structure lecture 4
Data structure lecture 4Data structure lecture 4
Data structure lecture 4
 
UNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdfUNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdf
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notations
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
UNIT V.docx
UNIT V.docxUNIT V.docx
UNIT V.docx
 
Big datacourse
Big datacourseBig datacourse
Big datacourse
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 
AD3251-Data Structures Design-Notes-Searching-Hashing.pdf
AD3251-Data Structures  Design-Notes-Searching-Hashing.pdfAD3251-Data Structures  Design-Notes-Searching-Hashing.pdf
AD3251-Data Structures Design-Notes-Searching-Hashing.pdf
 
L5 array
L5 arrayL5 array
L5 array
 
chapter7.ppt
chapter7.pptchapter7.ppt
chapter7.ppt
 
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufchapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
 
chapter7 Sorting.ppt
chapter7 Sorting.pptchapter7 Sorting.ppt
chapter7 Sorting.ppt
 

Plus de faizankhan260690

Plus de faizankhan260690 (7)

Science and Tech question for UPSC prelims
Science and Tech question for UPSC prelimsScience and Tech question for UPSC prelims
Science and Tech question for UPSC prelims
 
C applications
C applicationsC applications
C applications
 
Polarisation
PolarisationPolarisation
Polarisation
 
Friction problems
Friction problemsFriction problems
Friction problems
 
Friction
FrictionFriction
Friction
 
Friction [compatibility mode]
Friction [compatibility mode]Friction [compatibility mode]
Friction [compatibility mode]
 
Friction (2) [compatibility mode]
Friction (2) [compatibility mode]Friction (2) [compatibility mode]
Friction (2) [compatibility mode]
 

Dernier

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 

Dernier (20)

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 

sort search in C

  • 1. Searching Elements in an Array: Linear and Binary Search Spring Semester 2007 Programming and Data Structure 1
  • 2. Searching • Check if a given element (called key) occurs in the array. – Example: array of student records; rollno can be the key. • Two methods to be discussed: – If the array elements are unsorted. • Linear search – If the array elements are sorted. • Binary search Spring Semester 2007 Programming and Data Structure 2
  • 3. Linear Search Spring Semester 2007 Programming and Data Structure 3
  • 4. Basic Concept • Basic idea: – Start at the beginning of the array. – Inspect elements one by one to see if it matches the key. • Time complexity: – A measure of how long an algorithm takes to run. – If there are n elements in the array: • Best case: match found in first element (1 search operation) • Worst case: no match found, or match found in the last element (n search operations) • Average case: (n + 1) / 2 search operations Spring Semester 2007 Programming and Data Structure 4
  • 5. Contd. /* The function returns the array index where the match is found. It returns -1 if there is no match. */ int linear_search (int a[], int size, int key) { int pos = 0; while ((pos < size) && (a[pos] != key)) pos++; if (pos < size) return pos; /* Return the position of match */ return -1; /* No match found */ } Spring Semester 2007 Programming and Data Structure 5
  • 6. Contd. int x[]= {12, -3, 78, 67, 6, 50, 19, 10}; • Trace the following calls : search (x, 8, 6) ; Returns 4 search (x, 8, 5) ; Returns -1 Spring Semester 2007 Programming and Data Structure 6
  • 7. Binary Search Spring Semester 2007 Programming and Data Structure 7
  • 8. Basic Concept • Binary search works if the array is sorted. – Look for the target in the middle. – If you don’t find it, you can ignore half of the array, and repeat the process with the other half. • In every step, we reduce the number of elements to search in by half. Spring Semester 2007 Programming and Data Structure 8
  • 9. The Basic Strategy • What we want? – Find split between values larger and smaller than key: 0 n-1 x: <=key >key L R – Situation while searching: • Initially L and R contains the indices of first and last elements. – Look at the element at index [(L+R)/2]. • Move L or R to the middle depending on the outcome of test. Spring Semester 2007 Programming and Data Structure 9
  • 10. Contd. /* If key appears in x[0..size-1], return its location, pos such that x[pos]==key. If not found, return -1 */ int bin_search (int x[], int size, int key) { int L, R, mid; _________________; while ( ____________ ) { __________________; } _________________ ; } Spring Semester 2007 Programming and Data Structure 10
  • 11. The basic search iteration int bin_search (int x[], int size, int key) { int L, R, mid; _________________; while ( ____________ ) { mid = (L + R) / 2; if (x[mid] <= key) L = mid; else R = mid; } _________________ ; } Spring Semester 2007 Programming and Data Structure 11
  • 12. Loop termination int bin_search (int x[], int size, int key) { int L, R, mid; _________________; while ( L+1 != R ) { mid = (L + R) / 2; if (x[mid] <= key) L = mid; else R = mid; } _________________ ; } Spring Semester 2007 Programming and Data Structure 12
  • 13. Return result int bin_search (int x[], int size, int key) { int L, R, mid; _________________; while ( L+1 != R ) { mid = (L + R) / 2; if (x[mid] <= key) L = mid; else R = mid; } if (L >= 0 && x[L] == key) return L; else return -1; } Spring Semester 2007 Programming and Data Structure 13
  • 14. Initialization int bin_search (int x[], int size, int key) { int L, R, mid; L = -1; R = size; while ( L+1 != R ) { mid = (L + R) / 2; if (x[mid] <= key) L = mid; else R = mid; } if (L >= 0 && x[L] == key) return L; else return -1; } Spring Semester 2007 Programming and Data Structure 14
  • 15. Binary Search Examples Sorted array -17 -5 3 6 12 21 45 63 50 L= –1; R= 9; x[4]=12; Trace : L= –1; R=4; x[1]= –5; binsearch (x, 9, 3); L= 1; R=4; x[2]=3; L=2; R=4; x[3]=6; binsearch (x, 9, 145); L=2; R=3; return L; binsearch (x, 9, 45); We may modify the algorithm by checking equality with x[mid]. Spring Semester 2007 Programming and Data Structure 15
  • 16. Is it worth the trouble ? • Suppose that the array x has 1000 elements. • Ordinary search – If key is a member of x, it would require 500 comparisons on the average. • Binary search – after 1st compare, left with 500 elements. – after 2nd compare, left with 250 elements. – After at most 10 steps, you are done. Spring Semester 2007 Programming and Data Structure 16
  • 17. Time Complexity • If there are n elements in the array. – Number of searches required: log2n 2k= n, where k is the • For n = 64 (say). number of steps. – Initially, list size = 64. – After first compare, list size = 32. – After second compare, list size = 16. log264 = 6 – After third compare, list size = 8. log21024 = 10 – ……. – After sixth compare, list size = 1. Spring Semester 2007 Programming and Data Structure 17
  • 18. Sorting Spring Semester 2007 Programming and Data Structure 18
  • 19. The Basic Problem • Given an array x[0], x[1], ... , x[size-1] reorder entries so that x[0] <= x[1] <= . . . <= x[size-1] • List is in non-decreasing order. • We can also sort a list of elements in non- increasing order. Spring Semester 2007 Programming and Data Structure 19
  • 20. Example • Original list: 10, 30, 20, 80, 70, 10, 60, 40, 70 • Sorted in non-decreasing order: 10, 10, 20, 30, 40, 60, 70, 70, 80 • Sorted in non-increasing order: 80, 70, 70, 60, 40, 30, 20, 10, 10 Spring Semester 2007 Programming and Data Structure 20
  • 21. Sorting Problem • What we want ? – Data sorted in order size-1 0 x: Unsorted list Sorted list Spring Semester 2007 Programming and Data Structure 21
  • 22. Selection Sort Spring Semester 2007 Programming and Data Structure 22
  • 23. How it works? • General situation : 0 k size-1 x: smallest elements, sorted remainder, unsorted • Step : • Find smallest element, mval, in x[k..size-1] • Swap smallest element with x[k], then increase k. 0 k mval size-1 swap Spring Semester 2007 Programming and Data Structure 23
  • 24. Subproblem /* Yield index of smallest element in x[k..size-1];*/ int min_loc (int x[], int k, int size) { int j, pos; pos = k; for (j=k+1; j<size; j++) if (x[j] < x[pos]) pos = j; return pos; } Spring Semester 2007 Programming and Data Structure 24
  • 25. The main sorting function /* Sort x[0..size-1] in non-decreasing order */ int sel_sort (int x[], int size) { int k, m; for (k=0; k<size-1; k++) { m = min_loc (x, k, size); temp = a[k]; a[k] = a[m]; SWAP a[m] = temp; } } Spring Semester 2007 Programming and Data Structure 25
  • 26. int main() { int x[ ]={-45,89,-65,87,0,3,-23,19,56,21,76,-50}; int i; for(i=0;i<12;i++) printf("%d ",x[i]); printf("n"); -45 89 -65 87 0 3 -23 19 56 21 76 -50 sel_sort(x,12); -65 -50 -45 -23 0 3 19 21 56 76 87 89 for(i=0;i<12;i++) printf("%d ",x[i]); printf("n"); } Spring Semester 2007 Programming and Data Structure 26
  • 27. Example x: 3 12 -5 6 142 21 -17 45 x: -17 -5 3 6 12 21 142 45 x: -17 12 -5 6 142 21 3 45 x: -17 -5 3 6 12 21 142 45 x: -17 -5 12 6 142 21 3 45 x: -17 -5 3 6 12 21 45 142 x: -17 -5 3 6 142 21 12 45 x: -17 -5 3 6 142 21 12 45 Spring Semester 2007 Programming and Data Structure 27
  • 28. Analysis • How many steps are needed to sort n items ? – Total number of steps proportional to n2. – No. of comparisons? (n-1)+(n-2)+……+1= n(n-1)/2 Of the order of n2 – Worst Case? Best Case? Average Case? Spring Semester 2007 Programming and Data Structure 28
  • 29. Insertion Sort Spring Semester 2007 Programming and Data Structure 29
  • 30. How it works? • General situation : 0 i size-1 x: sorted remainder, unsorted i Compare and shift till x[i] is larger. i 0 j size-1 Spring Semester 2007 Programming and Data Structure 30
  • 31. Insertion Sort void insert_sort (int list[], int size) { int i,j,item; for (i=1; i<size; i++) { item = list[i] ; for (j=i-1; (j>=0)&& (list[j] > i); j--) list[j+1] = list[j]; list[j+1] = item ; } } Spring Semester 2007 Programming and Data Structure 31
  • 32. int main() { int x[ ]={-45,89,-65,87,0,3,-23,19,56,21,76,-50}; int i; for(i=0;i<12;i++) printf("%d ",x[i]); printf("n"); -45 89 -65 87 0 3 -23 19 56 21 76 -50 insert_sort(x,12); -65 -50 -45 -23 0 3 19 21 56 76 87 89 for(i=0;i<12;i++) printf("%d ",x[i]); printf("n"); } Spring Semester 2007 Programming and Data Structure 32
  • 33. Time Complexity • Number of comparisons and shifting: – Worst case? 1 + 2 + 3 + …… + (n-1) = n(n-1)/2 – Best case? 1 + 1 + …… to (n-1) terms = (n-1) Spring Semester 2007 Programming and Data Structure 33
  • 34. Bubble Sort Spring Semester 2007 Programming and Data Structure 34
  • 35. How it works? • The sorting process proceeds in several passes. – In every pass we go on comparing neighboring pairs, and swap them if out of order. – In every pass, the largest of the elements under considering will bubble to the top (i.e., the right). 10 5 17 11 -3 12 5 10 17 11 -3 12 5 10 17 11 -3 12 5 10 11 17 -3 12 5 10 11 -3 17 12 Largest 5 10 11 -3 12 17 Spring Semester 2007 Programming and Data Structure 35
  • 36. • How the passes proceed? – In pass 1, we consider index 0 to n-1. – In pass 2, we consider index 0 to n-2. – In pass 3, we consider index 0 to n-3. – …… – …… – In pass n-1, we consider index 0 to 1. Spring Semester 2007 Programming and Data Structure 36
  • 37. Bubble Sort void swap(int *x, int *y) void bubble_sort { (int x[], int n) int tmp = *x; { *x = *y; int i,j; *y = tmp; } for (i=n-1; i>0; i--) for (j=0; j<i; j++) if (x[j] > x[j+1]) swap(&x[j],&x[j+1]); } Spring Semester 2007 Programming and Data Structure 37
  • 38. int main() { int x[ ]={-45,89,-65,87,0,3,-23,19,56,21,76,-50}; int i; for(i=0;i<12;i++) printf("%d ",x[i]); printf("n"); -45 89 -65 87 0 3 -23 19 56 21 76 -50 bubble_sort(x,12); -65 -50 -45 -23 0 3 19 21 56 76 87 89 for(i=0;i<12;i++) printf("%d ",x[i]); printf("n"); } Spring Semester 2007 Programming and Data Structure 38
  • 39. Time Complexity • Number of comparisons : – Worst case? 1 + 2 + 3 + …… + (n-1) = n(n-1)/2 – Best case? Same Spring Semester 2007 Programming and Data Structure 39
  • 40. • How do you make best case with (n-1) comparisons only? – By maintaining a variable flag, to check if there has been any swaps in a given pass. – If not, the array is already sorted. Spring Semester 2007 Programming and Data Structure 40
  • 41. void bubble_sort (int x[], int n) { int i,j; int flag = 0; for (i=n-1; i>0; i--) { for (j=0; j<i; j++) if (x[j] > x[j+1]) { swap(&x[j],&x[j+1]); flag = 1; } if (flag == 0) return; } Spring Semester 2007 Programming and Data Structure 41
  • 42. Some Efficient Sorting Algorithms Spring Semester 2007 Programming and Data Structure 42
  • 43. • Two of the most popular sorting algorithms are based on divide-and-conquer approach. – Quick sort – Merge sort • Basic concept (divide-and-conquer method): sort (list) { if the list has length greater than 1 { Partition the list into lowlist and highlist; sort (lowlist); sort (highlist); combine (lowlist, highlist); } } Spring Semester 2007 Programming and Data Structure 43
  • 44. Quick Sort Spring Semester 2007 Programming and Data Structure 44
  • 45. How it works? • At every step, we select a pivot element in the list (usually the first element). – We put the pivot element in the final position of the sorted list. – All the elements less than or equal to the pivot element are to the left. – All the elements greater than the pivot element are to the right. Spring Semester 2007 Programming and Data Structure 45
  • 46. Partitioning 0 size-1 x: pivot Values smaller Values greater Perform Perform partitioning partitioning Spring Semester 2007 Programming and Data Structure 46
  • 47. Example 26 33 35 29 19 12 22 22 35 29 19 12 33 22 12 29 19 35 33 The partitioning 22 12 19 29 35 33 process 19 22 12 26 29 35 33 Recursively carry out the partitioning Spring Semester 2007 Programming and Data Structure 47
  • 48. void print (int x[], int low, int high) { int i; for(i=low; i<=high; i++) printf(" %d", x[i]); printf("n"); } void swap (int *a, int *b) { int tmp=*a; *a=*b; *b=tmp; } Spring Semester 2007 Programming and Data Structure 48
  • 49. void partition (int x[], int low, int high) { int i = low+1, j = high; int pivot = x[low]; if (low >= high) return; while (i<j) { while ((x[i]<pivot) && (i<high)) i++; while ((x[j]>=pivot) && (j>low)) j--; if (i<j) swap (&x[i], &x[j]); } if (j==high) { swap (&x[j], &x[low]); partition (x, low, high-1); } else if (i==low+1) partition (x, low+1, high); else { swap (&x[j], &x[low]); partition (x, low, j-1); partition (x, j+1, high); } } Spring Semester 2007 Programming and Data Structure 49
  • 50. int main (int argc, char *argv[]) { int *x; int i=0; int num; num = argc - 1; x = (int *) malloc(num * sizeof(int)); for (i=0; i<num; i++) x[i] = atoi(argv[i+1]); partition(x,0,num-1); printf(“Sorted list: "); print (x,0,num-1); } Spring Semester 2007 Programming and Data Structure 50
  • 51. Trace of Partitioning ./a.out 45 -56 78 90 -3 -6 123 0 -3 45 69 68 45 -56 78 90 -3 -6 123 0 -3 45 69 68 -6 -56 -3 0 -3 45 123 90 78 45 69 68 -56 -6 -3 0 -3 68 90 78 45 69 123 -3 0 -3 45 68 78 90 69 -3 0 69 78 90 Sorted list: -56 -6 -3 -3 0 45 45 68 69 78 90 123 Spring Semester 2007 Programming and Data Structure 51
  • 52. Time Complexity • Worst case: n2 ==> list is already sorted • Average case: n log2n • Statistically, quick sort has been found to be one of the fastest algorithms. Spring Semester 2007 Programming and Data Structure 52
  • 53. • Corollary of quick sort: – Given a list of numbers stored in an array, determine how many numbers are smaller than a given number p? – Given a list of integers (negative and non- negative), reorder the list so that all negative numbers precede the non-negative ones. Spring Semester 2007 Programming and Data Structure 53
  • 54. Merge Sort Spring Semester 2007 Programming and Data Structure 54
  • 55. Merge Sort Input Array Part-I Part-II Part-I Part-II Part-I Part-II Merge Split Sorted Arrays Spring Semester 2007 Programming and Data Structure 55
  • 56. Merging two sorted arrays pa pb a Sorted Array b Sorted Array 0 m 0 n c Merged sorted array 0 m+n-1 Move and copy elements pointed by pa if its value is smaller than the element pointed by pb in (m+n-1) operations; otherwise, copy elements pointed by pb . Spring Semester 2007 Programming and Data Structure 56
  • 57. Example • Initial array A contains 14 elements: – 66, 33, 40, 22, 55, 88, 60, 11, 80, 20, 50, 44, 77, 30 • Pass 1 :: Merge each pair of elements – (33, 66) (22, 40) (55, 88) (11, 60) (20, 80) (44, 50) (30, 70) • Pass 2 :: Merge each pair of pairs – (22, 33, 40, 66) (11, 55, 60, 88) (20, 44, 50, 80) (30, 77) • Pass 3 :: Merge each pair of sorted quadruplets – (11, 22, 33, 40, 55, 60, 66, 88) (20, 30, 44, 50, 77, 80) • Pass 4 :: Merge the two sorted subarrays to get the final list – (11, 20, 22, 30, 33, 40, 44, 50, 55, 60, 66, 77, 80, 88) Spring Semester 2007 Programming and Data Structure 57
  • 58. void merge_sort (int *a, int n) { int i, j, k, m; int *b, *c; if (n>1) { k = n/2; m = n-k; b = (int *) calloc(k,sizeof(int)); c = (int *) calloc(m,sizeof(int)); for (i=0; i<k; i++) b[i]=a[i]; for (j=k; j<n; j++) c[j-l]=a[j]; merge_sort (b, k); merge_sort (c, m); merge (b, c, a, k, m); free(b); free(c); } } Spring Semester 2007 Programming and Data Structure 58
  • 59. void merge (int *a, int *b, int *c, int m, int n) { int i, j, k, p; i = j = k = 0; do { if (a[i] < b[j]) { c[k]=a[i]; i=i+1; } else { c[k]=b[j]; j=j+1; } k++; } while ((i<m) && (j<n)); if (i == m) { for (p=j; p<n; p++) { c[k]=b[p]; k++; } } else { for (p=i; p<m; p++) { c[k]=a[p]; k++; } } } Spring Semester 2007 Programming and Data Structure 59
  • 60. main() { int i, num; int a[ ] = {-56,23,43,-5,-3,0,123,-35,87,56,75,80}; for (i=0;i<12;i++) printf ("%d ",a[i]); printf ("n"); merge_sort (a, 12); printf (“nSorted list:"); for (i=0;i<12;i++) printf (“ %d", a[i]); printf ("n"); } Spring Semester 2007 Programming and Data Structure 60