Download presentation
Presentation is loading. Please wait.
1
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006
2
2 Sequential Search Sequential search: search a list for an item Compare search item with other elements until either Item is found List has no more elements left Average number of comparisons made by the sequential search equals half the list size Good only for very short lists
3
3 int seqSearch(const int list[], int listLength, int searchItem) { int loc; for (loc = 0; loc < listLength; loc++) if (list[loc] == searchItem) return loc; return -1; }
4
4 Sorting a List: Bubble Sort Suppose list[0]...list[n - 1] is a list of n elements, indexed 0 to n – 1 Bubble sort algorithm: In a series of n - 1 iterations, compare successive elements, list[index] and list[index + 1] If list[index] is greater than list[index + 1], then swap them
5
5
6
6
7
7 void bubbleSort(int list[], int length) { int temp; int i, j; for (i = 0; i < length - 1; i++) { for (j = 0; j < length - 1 - i; j++) if (list[j] > list[j + 1]) { temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } Bubble Sort Algorithm for i = 0 to n-1 do for j = 0 to n - i - 1 do if list[j] > list[j+1] then Swap(list[j], list[j+1])
8
8 Sorting a List: Selection Sort Selection sort: rearrange list by selecting an element and moving it to its proper position Find the smallest (or largest) element and move it to the beginning (end) of the list On successive passes, locate the smallest item in the list starting from the next element
9
9 void selectionSort(int list[], int length) { int index; int smallestIndex; int minIndex; int temp; for (index = 0; index < length - 1; index++) { //Step a smallestIndex = index; for (minIndex = index + 1; minIndex < length; minIndex++) if (list[minIndex] < list[smallestIndex]) smallestIndex = minIndex; //Step b temp = list[smallestIndex]; list[smallestIndex] = list[index]; list[index] = temp; } Selection Sort Algorithm for (index = 0; index < length - 1; index++) { a.Find the location, smallestIndex, of the smallest element in list[index]…list[length-1]. a.Swap the smallest element with list[index]. That is, swap list[smallestIndex] with list[index] }
10
10 Sequential Search on an Ordered List General form of sequential search algorithm on a sorted list:
11
11 int seqOrderedSearch(const int list[], int listLength, int searchItem) { int loc;//Line 1 bool found = false;//Line 2 for (loc = 0; loc < listLength; loc++)//Line 3 if (list[loc] >= searchItem)//Line 4 { found = true;//Line 5 break;//Line 6 } if (found)//Line 7 if (list[loc] == searchItem)//Line 8 return loc;//Line 9 else//Line 10 return -1;//Line 11 else//Line 12 return -1;//Line 13 } Sequential Search on an Ordered List
12
12 Binary Search Binary search can be applied to sorted lists Uses the “ divide and conquer ” technique Compare search item to middle element mid= (first+last)/2 first is the starting index of the search list last is the ending index of the search list mid is the index of the middle element of the search list If search item is less than middle element, restrict the search to the lower half of the list Otherwise search the upper half of the list
13
13 Search 75 in the following list: search list
14
14 int binarySearch(const int list[], int listLength, int searchItem) { int first = 0; int last = listLength - 1; int mid; bool found = false; while(first <= last && !found) { mid = (first + last) / 2; if(list[mid] == searchItem) found = true; else if(list[mid] > searchItem) last = mid - 1; else first = mid + 1; } if(found) return mid; else return -1; } Binary Search
15
15 Binary Search (continued) Every iteration cuts size of search list in half If list L has 1000 items At most 11 iterations needed to determine if an item x is in list Every iteration makes 2 key (item) comparisons Binary search makes at most 22 key comparisons to determine if x is in L Sequential search makes 500 key comparisons (average) to if x is in L for the same size list
16
16 End of lecture 23 Thank you!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.