Lab#03 Searching and Sorting
Lab#03 Searching and Sorting
LAB # 03
Objective
The purpose of this lab is to implement the following searching and sorting techniques.
Binary Searching
Bubble Sort
Selection Sort
Insertion Sort
Theory
BINARY SEARCH
A binary search comes with the prerequisite that the data must be sorted. Search
a sorted array by repeatedly dividing the search interval in half. Low keep tracks of the
beginning of half and high keeps track of ending of half.
SORTING
Sorting algorithms simply puts elements (integers, numbers, strings, etc) of a list in a
certain order (increasing, decreasing, lexicographical, etc). There are many different
sorting algorithms, and each has its own advantages and limitations. We are
considering Bubble Sort, Selection sort and Insertion sort.
1. Set A [0] = -∞
2. Repeat Step 3-5 for k: =2-N
3. TEMP: =A[k] and PTR: =k-1
4. Repeat while TEMP <A [PTR]
a.Set A [PTR+1]:=A [PTR]
b.Set PTR: =PTR-1
[End of Step 4 loop]
5. Set A [PTR+1]:=TEMP
[End of Step 2 loop]
6. Return
Lab Task
1. Write a program for Selection sort that sorts an array containing numbers, prints
all the sort values of array each followed by its location.
2. Write a program that takes 10 numbers as input in an array. Sort the elements of
array by using Bubble sort. Print each iteration of the sorting process.
3. Write a program that takes 10 random numbers in an array. Sort the elements of
array by using Insertion sort. Print each iteration of the sorting process.
4. Use binary searching to search for any number entered by user. If the number is
not in an array then print search unsuccessful.
Home Task