{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

4 - Search-Sort

# 4 - Search-Sort - Searching • The process of finding...

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

Searching • The process of finding particular elements of an array or list • We will have a look at two searching techniques - Linear Search (simple searching technique) - Binary Search ( a more efficient searching technique)

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

View Full Document
Linear Search • Basic Idea: - Traverse the array/list and compare each element of the array/list with the element that we are looking for. • Since array/list is not in any particular order, it’s just as likely that the value be found at the start of the list as the last . • On average, program will have to compare value with half the elements of the array/list. • Works well for small or unsorted arrays and lists. • For large arrays and list; VERY INEFFICIENT
Linear Search Algorithm The algorithm searches for “Value” in the list “List” of size “ListSize”. “Loc” will contain the location in the list if found, else it will contain a –1. Algorithm: LinearSearch(List, ListSize, Value) 1. Declare and Initialise location pointer “Loc” to 0 2. While Value NOT found AND location pointer, “Loc” LESS THAN ListSize 2(a). Traverse up the list, “List” using our location pointer, “Loc”. 3. If “Loc” is EQUAL to ListSize 3(a). Element NOT found, so put –1 into “Loc” to indicate this (otherwise “Loc” contains the position of our Value)

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

View Full Document