Unsorted List Search Algorithm: Sequential Search - Prof. Craig R. Stewart, Lab Reports of Computer Science

In this document, students are introduced to the concept of sequential search, an algorithm for unsorted lists used in computer programming. The pseudocode for this search method and provides an example of why sorting a list is not always necessary or desirable. Students are encouraged to implement this algorithm in a lab exercise.

Typology: Lab Reports

Pre 2010

Uploaded on 08/01/2009

koofers-user-jrx-1
koofers-user-jrx-1 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to Programming (10061-001)
Lab Exercise for 5/1
List Search
In this lab, you will be writing a program that searches a list. There are several different algorithms
used for searching sequences in computer programming. The more optimal search algorithms require a
sorted list. However, this situation doesn't occur very often and in some cases cannot occur at all.
Programmers have algorithms for unsorted lists, but they do not run as quickly. The pseudocode for
sequential search, a search algorithm for unsorted lists, is as follows (NOTE - printing is only done
for testing purposes and to show the results of the function in IDLE):
1) Create a test list and give it some words or numbers.
2) Define a function to search the algorithm that takes in the element the user is searching for.
2.1. Create a counter and initialize it to 0.
2.2. While there are elements in the list that haven't been searched:
2.3. Get the index from the list equal to the counter.
2.4. If the list element matches the element being searched for, print and return the counter.
3) Since the element was not found in the list during the loop, print a message stating the element was
not found.
4) Return a number that is not an index for the list (for example, the length of the list).
Your flash card assignment holds a good example of a list that cannot be sorted. Sorting the list of
terms or definitions would mean that the other list would not be synced. For example:
terms = [“print”, “def”, “import”]
definitions = [“output keyword”, “header for functions”, “adds modules”]
You see here that the definition for terms[0] is definitions[0]. For terms[1], it's definitions[1] and so on.
If you were to sort the list of terms, you would end up with:
terms = [“def”, “import”, “print”]
definitions = [“output keyword”, “header for functions”, “adds modules”]
Now the terms and definitions are mixed up. It is much better to use the unsorted algorithm above than
to sort and use a sorted algorithm instead.
You MUST show me your work before you leave to earn participation points for the day!

Partial preview of the text

Download Unsorted List Search Algorithm: Sequential Search - Prof. Craig R. Stewart and more Lab Reports Computer Science in PDF only on Docsity!

Introduction to Programming (10061-001)

Lab Exercise for 5/

List Search In this lab, you will be writing a program that searches a list. There are several different algorithms used for searching sequences in computer programming. The more optimal search algorithms require a sorted list. However, this situation doesn't occur very often and in some cases cannot occur at all. Programmers have algorithms for unsorted lists, but they do not run as quickly. The pseudocode for sequential search , a search algorithm for unsorted lists, is as follows (NOTE - printing is only done for testing purposes and to show the results of the function in IDLE) :

  1. Create a test list and give it some words or numbers.
  2. Define a function to search the algorithm that takes in the element the user is searching for. 2.1. Create a counter and initialize it to 0. 2.2. While there are elements in the list that haven't been searched: 2.3. Get the index from the list equal to the counter. 2.4. If the list element matches the element being searched for, print and return the counter.
  3. Since the element was not found in the list during the loop, print a message stating the element was not found.
  4. Return a number that is not an index for the list (for example, the length of the list). Your flash card assignment holds a good example of a list that cannot be sorted. Sorting the list of terms or definitions would mean that the other list would not be synced. For example: terms = [“print”, “def”, “import”] definitions = [“output keyword”, “header for functions”, “adds modules”] You see here that the definition for terms[0] is definitions[0]. For terms[1], it's definitions[1] and so on. If you were to sort the list of terms, you would end up with: terms = [“def”, “import”, “print”] definitions = [“output keyword”, “header for functions”, “adds modules”] Now the terms and definitions are mixed up. It is much better to use the unsorted algorithm above than to sort and use a sorted algorithm instead. You MUST show me your work before you leave to earn participation points for the day!