14 - ICS103 Programming in C Lecture 14: Searching and...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 ICS103 Programming in C Lecture 14: Searching and Sorting
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Outline Searching Linear Search Algorithm Linear Search Implementation Binary Search Algorithm Binary Search Implementation Sorting Selection Sort Algorithm Selection Sort Implementation Bubble Sort Algorithm Bubble Sort Implementation
Background image of page 2
3 Introduction to Searching Searching means scanning through a list of items (in an array) to find if a particular one exists. It usually requires the user to specify the target item the item he wishes to locate If the target item is found, the item or its location (index) is returned, otherwise, an appropriate message or flag is returned. An important issue in processing a search request is response time. Some factors affecting response time are: The size of the array to search from The organization of data in the array; random or ordered The searching method or algorithm; linear or binary In this lecture, we study two searching methods; Linear Search and Binary Search.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Linear Search Algorithm This involves searching through the array sequentially until the target item is found or the array is exhausted. If the target is found, its location is returned, otherwise a flag such as –1 is returned. Here is the algorithm for Linear Search 1. Assume that the target has not been found 2. Start with initial array element 3. Repeat while the target is not found and there are more array elements 1. If the current element matches the target 1. Set a flag to indicate that the target has been found else 1. Advance to the next array element 1. If the target was found 1. Return the target index as the search result else 1. Return -1 as the search result
Background image of page 4
5 Linear Search Implementation #include <stdio.h> #define SIZE 8 int linear_search(double a[], double target, int size); void read_array(double a[], int size); int main(void) { double x[SIZE], target; int index; read_array(x, SIZE); printf("Enter Element to search for: "); scanf("%lf", &target); index = linear_search(x, target, SIZE);
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 13

14 - ICS103 Programming in C Lecture 14: Searching and...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online