SlideShare ist ein Scribd-Unternehmen logo
1 von 45
Searching an Array:
           Linear and Binary Search




January 5, 2013   Programming and   1
Searching

• Check if a given element (key) occurs in the
  array.
• Two methods to be discussed:
   – If the array elements are unsorted.
       • Linear search
   – If the array elements are sorted.
       • Binary search




January 5, 2013     Programming and        2
Linear Search

• Basic idea:
   – Start at the beginning of the array.
   – Inspect every element 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:
          (n + 1) / 2 search operations

January 5, 2013       Programming and                   3
Contd.
/* If key appears in a[0..size-1], return its location, pos, s.t.
   a[pos] == key. If key is not found, return -1 */
int linear_search (int a[], int size, int key)
{
        int pos = 0;
        while ((pos < size) && (a[pos] != key))
                pos++;
        if (pos<n)
                return pos; /* Return the position of match */
        return -1;            /* No match found */
}

 January 5, 2013     Programming and             4
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



January 5, 2013     Programming and         5
Binary Search

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




January 5, 2013    Programming and            6
The Basic Strategy

• What do we want? x[m]>key
                    no                   yes
           0                                 n-1
      x:       Elements in       Ascending order
           L                 m                     R

                    <=key              >key
          L                                      R
  – Look at [(L+R)/2]. Move L L R to the middle
                           R or
    depending on test.
  – Repeat search operation in the reduced interval.

  January 5, 2013    Programming and           7
Contd.
 /* If key appears in x[0..size-1], return its location, pos s.t.
     x[pos]==key. If not found, return -1 */

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


January 5, 2013         Programming and                    8
The basic search iteration
 /* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If
     not found, return -1 */

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


January 5, 2013           Programming and                       9
Loop termination
 /* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If
     not found, return -1 */

 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;
     }
     _________________ ;
 }


January 5, 2013           Programming and                       10
Return result
 /* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If
     not found, return -1 */

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

January 5, 2013           Programming and                       11
Initialization
 /* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If
     not found, return -1 */

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

January 5, 2013           Programming and                       12
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].

  January 5, 2013   Programming and           13
Is it worth the trouble ?

• Suppose there are 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.




January 5, 2013       Programming and               14
Time Complexity

• If there are n elements in the array.
   – Number of searches required:
       log2n                                2k= n,
• For n = 64 (say).            Where k is the 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.
   –   …….
   –   After sixth compare, list size = 1.

January 5, 2013     Programming and            15
Sorting:       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.



January 5, 2013      Programming and              16
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




January 5, 2013    Programming and        17
Sorting Problem

• What we want : Data sorted in order


     0                                  size-1
x:                 Unsorted list




                     Sorted list



January 5, 2013   Programming and   18
Selection Sort

• 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
January 5, 2013      Programming and                    19
Subproblem

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

int min_loc (int x[ ], int k, int size)
{
   int j, pos;     /* x[pos] is the smallest element found so far */
   pos = k;
   for (j=k+1; j<size; j++)
         if (x[j] < x[pos])
                  pos = j;
   return pos;
}

  January 5, 2013      Programming and              20
The main sorting function
/* Sort x[0..size-1] in non-decreasing order */

int selsort (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];
        a[m] = temp;
   }
}


January 5, 2013    Programming and                21
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

  January 5, 2013   Programming and        22
Analysis

• How many steps are needed to sort n things ?
   – 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?

January 5, 2013   Programming and           23
Insertion Sort

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

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


      0       j                                         size-1




January 5, 2013      Programming and                        24
Insertion Sorting



 void InsertSort (int list[], int size)
 {
    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 ;
    }
 }

January 5, 2013        Programming and               25
Insertion Sort

#define MAXN 100
void InsertSort (int list[MAXN], int size) ;
main () {
   int index, size;
   int numbers[MAXN];
   /* Get Input */
   size = readarray (numbers) ;
   printarray (numbers, size) ;
   InsertSort (numbers, size) ;
   printarray (numbers, size) ;
}



January 5, 2013        Programming and         26
Time Complexity

• Number of comparisons and shifting:
o            Worst Case?

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

o            Best Case?

    1+1+…… (n-1) times = (n-1)




January 5, 2013   Programming and   27
Bubble Sort


                       In every iteration heaviest element
                              drops at the bottom.




                  The bottom moves upward.




January 5, 2013      Programming and            28
Bubble Sort

#include <stdio.h>         void bubble_sort(int x[],int n)
                           {
void swap(int *x,int *y)     int i,j;
{
  int tmp=*x;              for(i=n-1;i>0;i--)
  *x=*y;                   for(j=0;j<i;j++)
  *y=tmp;                   if(x[j]>x[j+1]) swap(&x[j],&x[j+1]);
}
                           }


January 5, 2013      Programming and            29
Contd.
       int main(int argc, char *argv[])
       {
         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");
          bubble_sort(x,12);
         for(i=0;i<12;i++) printf("%d ",x[i]);
          printf("n");
       }
       -45 89 -65 87 0 3 -23 19 56 21 76 -50
       -65 -50 -45 -23 0 3 19 21 56 76 87 89
January 5, 2013    Programming and             30
Time Complexity

• Number of comparisons :
o            Worst Case?

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

o            Best Case?
             same.

How do you make best case with (n-1) comparisons only?



January 5, 2013   Programming and         31
Some Efficient Sorting Algorithms
• Two of the most popular sorting algorithms are based
  on divide-and-conquer approach.
   – Quick sort
   – Merge sort
• Basic concept:
       sort (list)
       {
         if the list has length greater than 1
         {
            Partition the list into lowlist and highlist;
            sort (lowlist);
            sort (highlist);
            combine (lowlist, highlist);
         }
       }


January 5, 2013         Programming and                     33
Quicksort

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




January 5, 2013   Programming and            34
Partitioning


       0                                  size-1
 x:
   pivot

           Values smaller         Values greater



              Perform
                                     Perform
             partitioning           partitioning



January 5, 2013       Programming and              35
Quick Sort: Example
#include <stdio.h>                     void swap(int *a,int *b)
#include <stdlib.h>                    {
                                         int tmp=*a;
void print(int x[],int low,int high)     *a=*b; *b=tmp;
{                                      }
  int i;

    for(i=low;i<=high;i++)
    printf(" %d", x[i]);
    printf("n");
}

January 5, 2013     Programming and             36
Contd.
void partition(int x[],int low,int high)
                                        if (j==high) {
{
                                        swap(&x[j],&x[low]);
 int i=low+1,j=high;
                                        partition(x,low,high-1);
 int pivot=x[low];
                                          }
 if(low>=high) return;
                                        else
 while(i<j)
                                        if (i==low+1)
 {
                                        partition(x,low+1,high);
  while ((x[i]<pivot) && (i<high)) i++;
                                         else {
  while ((x[j]>=pivot) && (j>low)) j--;
                                           swap(&x[j],&x[low]);
  if(i<j) swap(&x[i],&x[j]);
                                           partition(x,low,j-1);
 }
                                          partition(x,j+1,high);
                                         }
    January 5, 2013     Programming and }         37
Contd:
int main(int argc,char *argv[])
                                              printf("Input: ");
{
                                             for(i=0; i<num; i++)
  int *x;
                                               printf(" %d", x[i]);
  int i=0;
                                               printf("n");
  int num;
                                              partition(x,0,num-1);
                                              printf("Output: ");
 num=argc-1;
                                              for(i=0; i<num; i++)
 x=(int *) malloc(num * sizeof(int));
                                               printf(" %d", x[i]);
                                               printf("n");
 for(i=0;i<num; i++)
                                            }
  x[i]=atoi(argv[i+1]);

    January 5, 2013       Programming and             38
Trace of Partitioning

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

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

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

January 5, 2013     Programming and             39
Time Complexity

• Partitioning with n elements.
   - No. of comparisons:
             n-1             Choice of pivot element affects
                              the time complexity.
• Worst Case Performance:
 (n-1)+(n-2)+(n-3)+………. +1= n(n-1)/2
• Best Case performance:
  (n-1)+2((n-1)/2-1)+4(((n-1)/2-1)-1)/2-1) .. k steps
  = O(n. log(n))
                                               2k=n
January 5, 2013   Programming and         40
Merge Sort

                                Input Array




             Part-I                                Part-II


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




            Merge                                      Split
         Sorted arrays

January 5, 2013       Programming and                          41
Merging two sorted arrays
         pa                                        pb


a             Sorted Array            b            Sorted Array

    0                         l           0                          m



          c                  Merged sorted array

              0                                                   l+m-1


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

January 5, 2013          Programming and                 42
Merge Sort
      #include <stdio.h>
      #include <stdlib.h>
       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(“n The sorted sequence follows n");
         for(i=0;i<12;i++) printf("%d ",a[i]); printf("n");
      }

January 5, 2013    Programming and              43
/* Recursive function for dividing an array a[0..n-1]
   into two halves and sort them and merge them
                  subsequently. */

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

January 5, 2013     Programming and             44
/* Merging of two sorted arrays a[0..m-1] and b[0..n-1]
            into a single array c[0..m+n-1] */

void merge(int *a,int *b,int *c,
                                     if(i==m){
              int m,int n)
                                     for(l=j;l<n;l++){ c[k]=b[l]; k++;}
{int i,j,k,l;
                                     }
                                     else{
i=j=k=0;
                                     for(l=i;l<m;l++){c[k]=a[l]; k++;}
                                     }
do{
                                     }
if(a[i]<b[j]){ c[k]=a[i]; i=i+1; }
else{ c[k]=b[j]; j=j+1;}
k++;
                                   Pointer movement and copy operations.
} while((i<m)&&(j<n));

    January 5, 2013     Programming and             45
Splitting Trace
                  -56 23 43 -5 -3 0 123 -35 87 56 75 80

       -56 23 43 -5 -3 0                  123 -35 87 56 75 80

                                          123 -35 87         56 75 80
      -56 23 43      -5 -3 0
                                        123        -35 87          75 80
                             -3 0                            56
-56        23 43                                -35     87
                        -3          0
                                                                75     80
            23         43
                                    Worst Case: O(n.log(n))

                        -56 -35 -5 -3 0 23 43 56 75 80 87 123

January 5, 2013        Programming and                 46

Weitere ähnliche Inhalte

Was ist angesagt?

SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsMohamed Loey
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting AlgorithmsRahul Jamwal
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1Kumar
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithmNeoClassical
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhithRj Juhith
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0BG Java EE Course
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sortingguest2cb109
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary searchZia Ush Shamszaman
 
Chapter 14 Searching and Sorting
Chapter 14 Searching and SortingChapter 14 Searching and Sorting
Chapter 14 Searching and SortingMuhammadBakri13
 
7 searching injava-binary
7 searching injava-binary7 searching injava-binary
7 searching injava-binaryirdginfo
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...Dharmendra Prasad
 

Was ist angesagt? (20)

Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
 
Binary Search
Binary SearchBinary Search
Binary Search
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
Unit 2 algorithm
Unit   2 algorithmUnit   2 algorithm
Unit 2 algorithm
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
Chapter 14 Searching and Sorting
Chapter 14 Searching and SortingChapter 14 Searching and Sorting
Chapter 14 Searching and Sorting
 
Sorting
SortingSorting
Sorting
 
7 searching injava-binary
7 searching injava-binary7 searching injava-binary
7 searching injava-binary
 
Sorting algorithm
Sorting algorithmSorting algorithm
Sorting algorithm
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
 
Big o notation
Big o notationBig o notation
Big o notation
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 

Ähnlich wie L10 sorting-searching

In the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdfIn the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdfarpitaeron555
 
Module 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptxModule 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptxGaneshRaghu4
 
Array 2
Array 2Array 2
Array 2Abbott
 
chapter1.pdf ......................................
chapter1.pdf ......................................chapter1.pdf ......................................
chapter1.pdf ......................................nourhandardeer3
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsJulie Iskander
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfShiwani Gupta
 
Data structure lecture 4
Data structure lecture 4Data structure lecture 4
Data structure lecture 4Kumar
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptxchouguleamruta24
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfMustafaJutt4
 
Data Structure Searching.pptx
Data Structure Searching.pptxData Structure Searching.pptx
Data Structure Searching.pptxSourabhTaneja4
 

Ähnlich wie L10 sorting-searching (20)

sort search in C
 sort search in C  sort search in C
sort search in C
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 
In the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdfIn the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdf
 
Module 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptxModule 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptx
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Complexity
ComplexityComplexity
Complexity
 
binary search
binary searchbinary search
binary search
 
Array 2
Array 2Array 2
Array 2
 
chapter1.pdf ......................................
chapter1.pdf ......................................chapter1.pdf ......................................
chapter1.pdf ......................................
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data Structures 7
Data Structures 7Data Structures 7
Data Structures 7
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
 
L12 complexity
L12 complexityL12 complexity
L12 complexity
 
Data structure lecture 4
Data structure lecture 4Data structure lecture 4
Data structure lecture 4
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
Algorithms.
Algorithms. Algorithms.
Algorithms.
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdf
 
Data Structure Searching.pptx
Data Structure Searching.pptxData Structure Searching.pptx
Data Structure Searching.pptx
 

Mehr von mondalakash2012 (12)

Snns
SnnsSnns
Snns
 
Sn reaction
Sn reactionSn reaction
Sn reaction
 
Part i
Part iPart i
Part i
 
Extra problem for 1st yr
Extra problem for 1st yrExtra problem for 1st yr
Extra problem for 1st yr
 
Elimination
EliminationElimination
Elimination
 
L11 tree
L11 treeL11 tree
L11 tree
 
L8 file
L8 fileL8 file
L8 file
 
L6 structure
L6 structureL6 structure
L6 structure
 
L4 functions
L4 functionsL4 functions
L4 functions
 
L3 control
L3 controlL3 control
L3 control
 
L2 number
L2 numberL2 number
L2 number
 
Struct examples
Struct examplesStruct examples
Struct examples
 

L10 sorting-searching

  • 1. Searching an Array: Linear and Binary Search January 5, 2013 Programming and 1
  • 2. Searching • Check if a given element (key) occurs in the array. • Two methods to be discussed: – If the array elements are unsorted. • Linear search – If the array elements are sorted. • Binary search January 5, 2013 Programming and 2
  • 3. Linear Search • Basic idea: – Start at the beginning of the array. – Inspect every element 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: (n + 1) / 2 search operations January 5, 2013 Programming and 3
  • 4. Contd. /* If key appears in a[0..size-1], return its location, pos, s.t. a[pos] == key. If key is not found, return -1 */ int linear_search (int a[], int size, int key) { int pos = 0; while ((pos < size) && (a[pos] != key)) pos++; if (pos<n) return pos; /* Return the position of match */ return -1; /* No match found */ } January 5, 2013 Programming and 4
  • 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 January 5, 2013 Programming and 5
  • 6. Binary Search • 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. January 5, 2013 Programming and 6
  • 7. The Basic Strategy • What do we want? x[m]>key no yes 0 n-1 x: Elements in Ascending order L m R <=key >key L R – Look at [(L+R)/2]. Move L L R to the middle R or depending on test. – Repeat search operation in the reduced interval. January 5, 2013 Programming and 7
  • 8. Contd. /* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ int bin_search (int x[], int size, int key) { int L, R, mid; _________________; while ( ____________ ) { __________________; } _________________ ; } January 5, 2013 Programming and 8
  • 9. The basic search iteration /* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ int bin_search (int x[], int size, int key) { int L, R, mid; _________________; while ( ____________ ) { mid = (L + R) / 2; if (x[mid] > key) R = mid; else L = mid; } _________________ ; } January 5, 2013 Programming and 9
  • 10. Loop termination /* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ 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; } _________________ ; } January 5, 2013 Programming and 10
  • 11. Return result /* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ 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; } January 5, 2013 Programming and 11
  • 12. Initialization /* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ 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; } January 5, 2013 Programming and 12
  • 13. 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]. January 5, 2013 Programming and 13
  • 14. Is it worth the trouble ? • Suppose there are 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. January 5, 2013 Programming and 14
  • 15. Time Complexity • If there are n elements in the array. – Number of searches required: log2n 2k= n, • For n = 64 (say). Where k is the 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. – ……. – After sixth compare, list size = 1. January 5, 2013 Programming and 15
  • 16. Sorting: 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. January 5, 2013 Programming and 16
  • 17. 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 January 5, 2013 Programming and 17
  • 18. Sorting Problem • What we want : Data sorted in order 0 size-1 x: Unsorted list Sorted list January 5, 2013 Programming and 18
  • 19. Selection Sort • 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 January 5, 2013 Programming and 19
  • 20. Subproblem /* Yield location of smallest element in x[k .. size-1];*/ int min_loc (int x[ ], int k, int size) { int j, pos; /* x[pos] is the smallest element found so far */ pos = k; for (j=k+1; j<size; j++) if (x[j] < x[pos]) pos = j; return pos; } January 5, 2013 Programming and 20
  • 21. The main sorting function /* Sort x[0..size-1] in non-decreasing order */ int selsort (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]; a[m] = temp; } } January 5, 2013 Programming and 21
  • 22. 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 January 5, 2013 Programming and 22
  • 23. Analysis • How many steps are needed to sort n things ? – 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? January 5, 2013 Programming and 23
  • 24. Insertion Sort • General situation : 0 i size-1 x: smallest elements, sorted remainder, unsorted i Compare and Shift till x[i] is larger. i 0 j size-1 January 5, 2013 Programming and 24
  • 25. Insertion Sorting void InsertSort (int list[], int size) { 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 ; } } January 5, 2013 Programming and 25
  • 26. Insertion Sort #define MAXN 100 void InsertSort (int list[MAXN], int size) ; main () { int index, size; int numbers[MAXN]; /* Get Input */ size = readarray (numbers) ; printarray (numbers, size) ; InsertSort (numbers, size) ; printarray (numbers, size) ; } January 5, 2013 Programming and 26
  • 27. Time Complexity • Number of comparisons and shifting: o Worst Case? 1+2+3+ …… +(n-1) = n(n-1)/2 o Best Case? 1+1+…… (n-1) times = (n-1) January 5, 2013 Programming and 27
  • 28. Bubble Sort In every iteration heaviest element drops at the bottom. The bottom moves upward. January 5, 2013 Programming and 28
  • 29. Bubble Sort #include <stdio.h> void bubble_sort(int x[],int n) { void swap(int *x,int *y) int i,j; { int tmp=*x; for(i=n-1;i>0;i--) *x=*y; for(j=0;j<i;j++) *y=tmp; if(x[j]>x[j+1]) swap(&x[j],&x[j+1]); } } January 5, 2013 Programming and 29
  • 30. Contd. int main(int argc, char *argv[]) { 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"); bubble_sort(x,12); for(i=0;i<12;i++) printf("%d ",x[i]); printf("n"); } -45 89 -65 87 0 3 -23 19 56 21 76 -50 -65 -50 -45 -23 0 3 19 21 56 76 87 89 January 5, 2013 Programming and 30
  • 31. Time Complexity • Number of comparisons : o Worst Case? 1+2+3+ …… +(n-1) = n(n-1)/2 o Best Case? same. How do you make best case with (n-1) comparisons only? January 5, 2013 Programming and 31
  • 32. Some Efficient Sorting Algorithms • Two of the most popular sorting algorithms are based on divide-and-conquer approach. – Quick sort – Merge sort • Basic concept: sort (list) { if the list has length greater than 1 { Partition the list into lowlist and highlist; sort (lowlist); sort (highlist); combine (lowlist, highlist); } } January 5, 2013 Programming and 33
  • 33. Quicksort • 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. January 5, 2013 Programming and 34
  • 34. Partitioning 0 size-1 x: pivot Values smaller Values greater Perform Perform partitioning partitioning January 5, 2013 Programming and 35
  • 35. Quick Sort: Example #include <stdio.h> void swap(int *a,int *b) #include <stdlib.h> { int tmp=*a; void print(int x[],int low,int high) *a=*b; *b=tmp; { } int i; for(i=low;i<=high;i++) printf(" %d", x[i]); printf("n"); } January 5, 2013 Programming and 36
  • 36. Contd. void partition(int x[],int low,int high) if (j==high) { { swap(&x[j],&x[low]); int i=low+1,j=high; partition(x,low,high-1); int pivot=x[low]; } if(low>=high) return; else while(i<j) if (i==low+1) { partition(x,low+1,high); while ((x[i]<pivot) && (i<high)) i++; else { while ((x[j]>=pivot) && (j>low)) j--; swap(&x[j],&x[low]); if(i<j) swap(&x[i],&x[j]); partition(x,low,j-1); } partition(x,j+1,high); } January 5, 2013 Programming and } 37
  • 37. Contd: int main(int argc,char *argv[]) printf("Input: "); { for(i=0; i<num; i++) int *x; printf(" %d", x[i]); int i=0; printf("n"); int num; partition(x,0,num-1); printf("Output: "); num=argc-1; for(i=0; i<num; i++) x=(int *) malloc(num * sizeof(int)); printf(" %d", x[i]); printf("n"); for(i=0;i<num; i++) } x[i]=atoi(argv[i+1]); January 5, 2013 Programming and 38
  • 38. Trace of Partitioning ./a.out 45 -56 78 90 -3 -6 123 0 -3 45 69 68 Input: 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 Output: -56 -6 -3 -3 0 45 45 68 69 78 90 123 January 5, 2013 Programming and 39
  • 39. Time Complexity • Partitioning with n elements. - No. of comparisons: n-1 Choice of pivot element affects the time complexity. • Worst Case Performance: (n-1)+(n-2)+(n-3)+………. +1= n(n-1)/2 • Best Case performance: (n-1)+2((n-1)/2-1)+4(((n-1)/2-1)-1)/2-1) .. k steps = O(n. log(n)) 2k=n January 5, 2013 Programming and 40
  • 40. Merge Sort Input Array Part-I Part-II Part-I Part-II Part-I Part-II Merge Split Sorted arrays January 5, 2013 Programming and 41
  • 41. Merging two sorted arrays pa pb a Sorted Array b Sorted Array 0 l 0 m c Merged sorted array 0 l+m-1 Move and copy elements pointed by pa if its value is smaller than the element pointed by pb in (l+m-1) operations and otherwise. January 5, 2013 Programming and 42
  • 42. Merge Sort #include <stdio.h> #include <stdlib.h> 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(“n The sorted sequence follows n"); for(i=0;i<12;i++) printf("%d ",a[i]); printf("n"); } January 5, 2013 Programming and 43
  • 43. /* Recursive function for dividing an array a[0..n-1] into two halves and sort them and merge them subsequently. */ void merge_sort(int *a,int n) for(i=0;i<l;i++) b[i]=a[i]; { for(j=l;j<n;j++) c[j-l]=a[j]; int i,j,l,m; int *b, *c; merge_sort(b,l); merge_sort(c,m); if(n>1){ merge(b,c,a,l,m); l=n/2; m=n-l; free(b); free(c); b=(int *) calloc(l,sizeof(int)); } c=(int *) calloc(m,sizeof(int)); } January 5, 2013 Programming and 44
  • 44. /* Merging of two sorted arrays a[0..m-1] and b[0..n-1] into a single array c[0..m+n-1] */ void merge(int *a,int *b,int *c, if(i==m){ int m,int n) for(l=j;l<n;l++){ c[k]=b[l]; k++;} {int i,j,k,l; } else{ i=j=k=0; for(l=i;l<m;l++){c[k]=a[l]; k++;} } do{ } if(a[i]<b[j]){ c[k]=a[i]; i=i+1; } else{ c[k]=b[j]; j=j+1;} k++; Pointer movement and copy operations. } while((i<m)&&(j<n)); January 5, 2013 Programming and 45
  • 45. Splitting Trace -56 23 43 -5 -3 0 123 -35 87 56 75 80 -56 23 43 -5 -3 0 123 -35 87 56 75 80 123 -35 87 56 75 80 -56 23 43 -5 -3 0 123 -35 87 75 80 -3 0 56 -56 23 43 -35 87 -3 0 75 80 23 43 Worst Case: O(n.log(n)) -56 -35 -5 -3 0 23 43 56 75 80 87 123 January 5, 2013 Programming and 46