Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Lab#03: Searching and sorting in a Linear Array SSUET/QR/114

LAB # 03

Searching and Sorting in a Linear Array

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.

Algorithm for Binary Search

1. [Initialize Variables] Set BEG := 0, END := BINARRAY.length – 1 AND


MID: = (BEG + END)/2
2. [Search for the ITEM]
Repeat while BEG <= END OR BINARRAY [MID]:≠ ITEM
If ITEM<BINARRAY [MID] Then
Set END: = MID – 1 andMID: = (BEG + END)/2
Else If ITEM>BINARRAY [MID] Then
Set BEG: = MID + 1 andMID: = (BEG + END)/2
Else If ITEM: =BINARRAY [MID] Then
Set ITLOC: = MID and Return ITLOC
[End of If Structure]
[End of Loop]
3. [Unsuccessful Search] Set ITLOC: = -1 and Return ITLOC
4. Exit

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.

CE-205: Data Structures and Algorithms Page 17


Lab#03: Searching and sorting in a Linear Array SSUET/QR/114

Algorithm for Bubble Sort

1. Repeat step 2 -3 for K:=1 to N-1


2. Set PTR :=1
3. Repeat While PTR≤N-K
a) If DATA[PTR] > DATA[PTR+1], then
Interchange DATA [PTR] and DATA [PTR+1]
[End of if structure]
b) Set PTR:= PTR+1
[End of Step 3 loop]
[End of Step 1 loop]
4. Exit

Algorithm for Selection Sort


SELECTION (A, N)

This algorithm sorts the array A with N elements.

1. Repeat step 2 to 6 for i = 1 to N


2. Set Min = i
3. Repeat step 4 and 5 for j= i+1 to N
4. If List[j] < List [Min] then
5. Set Min = j
6. Swap (List[i], List[Min])
7. End

Algorithm for Insertion Sort


InsertionSort (LA, N)

This algorithm sorts the array LA with N elements.

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

CE-205: Data Structures and Algorithms Page 18


Lab#03: Searching and sorting in a Linear Array SSUET/QR/114

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

1. Declare an array of size n to store account balances. Initialize with values 0 to


100000 and sort Account No’s according to highest balance values by using all
sorting techniques for e.g.:
Account No. 3547 Balance 28000

Account No. 1245 Balance 12000

CE-205: Data Structures and Algorithms Page 19

You might also like