Computer Science 1622
Computer Science II

Laboratory Assignment 5
Performance of Sorting and Searching Arrays
Due at the end of Lab

Introduction

Sorting is the process by which a set of N elements are arranged in an order based on the values in the array. Searching is the process used to find the location of a target element in an array. There are several different algorithms that can be used for sorting (Selection, Insertion, Merge) and searching (Sequential, Binary) arrays.

To choose an algorithm we attempt to characterize the performance of the algorithm with respect to an array of size N. We then determine which operations are critical for each type of problem. For example, in sorting we can characterize the performance of a sorting algorithm by (1) the number of times it compares an element in the array to another value or (2) the number of times it moves an element from or to a position in the array. For searching we generally characterize the performance of the algorithm by how many times we compare a value to an element in the array. Once we have chosen what operation to count we then come up with a formula for counting that operation by examining the algorithm. Then we use Big-O notation to reduce the formula to its key term and compare the key terms of the algorithms amongst which we are choosing.

A complimentary technique to this abstract analysis is to implement the algorithm in question and then to add code to count the operations of interest. We can then graph the performance of our algorithm empirically and see how well our experiments agree with our analysis.

In this lab we are going to empirically examine the performance of two algorithms: insertion sort and binary search.

The Program

The following program prompts the user for information that is used to generate a test array (the user is asked how big an array they want and whether or not they want an initially sorted array). The program then sorts the array using insertion sort. Following this the program prompts the user for a number to search the array for and searches the array using binary search for that number.

Your job is to add code to count the number of comparison operations that are made in (1) the insertion sort and (2) the binary search and to report the two values. To do so you will need to add local variables and possibly parameters to one or more of the functions in the program.

Once you have added these counts, run the program several times for different types and sizes of arrays to see how well the analysis in class agrees with the numbers your program reports (and note your conclusions on your output).

The Code

#include <stdio.h>
#include <stdlib.h>

#define ASIZE 50

void generateArray(int A[], int *N) {
  char presorted;
  int i;

  do {
    printf("How big should the array be (enter a value between 1 and %d)? ",ASIZE);
    scanf("%d",N);
  } while ((*N < 1) || (*N > ASIZE));

  printf("Do you want an already sorted array (Y for yes)? ");
  scanf(" %c",&presorted);

  if ((presorted == 'Y') || (presorted == 'y')) {
    A[0] = rand() % 200;
    for (i = 1; i < *N; i++)
      A[i] = A[i-1] + (rand() % 200);
  }
  else { /* Random array */
    for (i = 0; i < *N; i++)
      A[i] = rand() % 1000;
  }
}

void printArray(int A[], int N) {
  int i;

  for (i = 0; i < N; i++)
    printf("%-5d ",A[i]);
  printf("\n\n");
}

void insertIntoSegment(int A[], int J) { 
  int K; 
  int temp = A[J]; 

  for (K = J; (K > 0) && (A[K-1] > temp); K--) 
    A[K] = A[K-1]; 

  A[K] = temp; 
} 

void sort(int A[], int N) { 
  int J; 

  for (J = 1; J < N; J++) 
    insertIntoSegment(A,J); 
}

int search(int A[], int N, int search_val) {
  int first = 0; 
  int last = N - 1; 
  int mid = (first + last) / 2; 

  while ((A[mid] != search_val) && (first <= last)) { 
    if (A[mid] > search_val) 
      last = mid - 1; 
    else 
      first = mid + 1; 
    mid = (first + last) / 2; 
  }
  if (A[mid] == search_val) return mid;
  return -1;
}

void main() {
  int A[ASIZE];
  int N;
  int search_val;
  int position;

  generateArray(A,&N);
  printf("The original array:\n  ");
  printArray(A,N);

  sort(A,N);
  printf("The array after sorting:\n  ");
  printArray(A,N);

  printf("What number should I search the array for: ");
  scanf("%d",&search_val);
  position = search(A,N,search_val);
  if (position == -1)
    printf("  Value (%d) not found!\n",search_val);
  else
    printf("  Value (%d) found at position %d.\n",search_val,position);
}

What to turn in

Turn in a hard copy of your final program. Also, turn in a copy of the output for your program showing the number of comparisons for several differently sized random and sorted arrays. Remember to note on your output how well the resulting counts agree with the analysis in class of the performance of the algorithms.