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

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

1 ICS103 Programming in C Lecture 14: Searching and Sorting

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

View Full Document
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
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.

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

View Full Document
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
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);

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 05/09/2010 for the course ICS 103 taught by Professor Baleh during the Spring '10 term at Abilene Christian University.

### 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
Ask a homework question - tutors are online