LAB2
LAB2
1
Engr. Muhammad Arslan Rafique
Lab 02: To implement bubble sort, selection sort and insertion sort algorithms
on
an array type data structure.
Objective: To learn how to implement different sorting algorithms on an array type data
structure.
Sorting
Arrays:
The process of arranging data in a specified order is called sorting. Numeric type data may be
arranged either in ascending or in descending order. Similarly, character type data type may be
arranged in alphabetical order.
There are different methods to sort data into a list. The most commonly used methods
are:
i. Bubble Sort
ii. Selection Sort
iii. Insertion Sort
Bubble
Sort:
The bubble sort method is used to arrange values of an array in ascending or in descending
order.
To arrange an array in ascending order, two neighboring elements are compared. If one element is
larger than the other, the two are exchanged. Through the exchange of elements, the larger value
slowly floats or bubbles up to the top.
4 9 1 3
As n = 4;
So total iterations = 3
Iteration 1:
4 9 1 3
4 9 1 3
4 1 9 3
4 1 3 9
Iteration 2:
4 1 3 9
2
Engr. Muhammad Arslan Rafique
1 4 3 9
1 3 4 9
3
Engr. Muhammad Arslan Rafique
Iteration 3:
1 3 4 9
1 3 4 9
Algorithm:
begin BubbleSort(list)
return list
end BubbleSort
loop = list.count;
end for
Following method is used to sort an array in ascending order using selection sort:
In the 1st iteration the value in the first element is assumed to be the smallest. Then the next smallest
element is found in the array. This value is interchanged with the 1st element. Now the 1st element of
the array has the smallest value.
In the 2nd iteration, the smallest value from the second element to last element of list is found. This
value is interchanged with the second element of the array.
For the first position in the sorted list, the whole list is scanned sequentially. The first position where
14 is stored presently, we search the whole list and find that 10 is the lowest value.
So we replace 14 with 10. After one iteration 10, which happens to be the minimum value in the list,
appears in the first position of the sorted list.
For the second position, where 33 is residing, we start scanning the rest of the list in a linear manner.
We find that 14 is the second lowest value in the list and it should appear at the second place. We
swap these values.
After two iterations, two least values are positioned at the beginning in a sorted manner.
The same process is applied to the rest of the items in the array.
5
Engr. Muhammad Arslan Rafique
Following is a pictorial depiction of the entire sorting process −
Algorithm:
6
Engr. Muhammad Arslan Rafique
procedure selection sort
list : array of items
n : size of list
for i = 1 to n - 1
/* set current element as minimum*/
min = i
for j = i+1 to n
if list[j] < list[min] then
min = j;
end if
end for
end procedure
Insertion Sort:
The insertion sort method is used to arrange values of an array in ascending or in descending order.
Step 1: The second element of an array is compared with the elements that appears before it
(only first element in this case). If the second element is smaller than first element, second
element is inserted in the position of first element. After first step, first two elements of an array
will be sorted.
Step 2: The third element of an array is compared with the elements that appears before it (first and
second element). If third element is smaller than first element, it is inserted in the position of first
element. If third element is larger than first element but, smaller than second element, it is inserted
in the position of second element. If third element is larger than both the elements, it is kept in
the position as it is. After second step, first three elements of an array will be sorted.
Step 3: Similarly, the fourth element of an array is compared with the elements that appear
before it (first, second and third element) and the same procedure is applied and that element is
inserted in the proper position. After third step, first four elements of an array will be sorted.
If there are n elements to be sorted. Then, this procedure is repeated n-1 times to get sorted list of
array.
7
Engr. Muhammad Arslan Rafique
Figure: Sorting Array in Ascending Order Using Insertion Sort Algorithm
Algorithm:
Step 1 − If it is the first element, it is already sorted. return 1;
Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted
8
Engr. Muhammad Arslan Rafique
/*locate hole position for the element to be inserted */
end for
end procedure
Pointers:
A pointer is a variable that is used to store a memory address. Variables contain a value, while
pointers contain a memory address. Pointers help in allocating memory dynamically. Pointers are
used to create and manipulate data structures such as linked lists, queues, stacks, trees, etc.
Syntax:
type* variable_name;
int* a;
float* b;
void* c;
There are two operators of pointers:
Dereference *
var1 = 5;
ptr = &var1;
var2 = *ptr;
9
Engr. Muhammad Arslan Rafique
Pictorially, it can be shown as:
10
Engr. Muhammad Arslan Rafique
Lab Task:
1. Write a program to sort the following list by using bubble sorting method.
4 9 1 3
INPUT:
OUTPUT:
11
Engr. Muhammad Arslan Rafique
2. Write a program which takes names of five countries as input and prints them
in alphabetical order using bubble sorting method.
INPUT:
OUTPUT:
12
Engr. Muhammad Arslan Rafique
3. Write a program to sort the following list by using selection sorting method.
14 19 11 34
INPUT:
OUTPUT:
13
Engr. Muhammad Arslan Rafique
4. Write a program which takes names of five countries as input and prints them
in alphabetical order using selection sorting method.
INPUT:
OUTPUT:
14
Engr. Muhammad Arslan Rafique
5. Write a program to sort the following list by using insertion sorting method.
4 91 106 32
INPUT:
OUTPUT:
15
Engr. Muhammad Arslan Rafique
6. Write a program which takes names of five countries as input and prints them
in alphabetical order using insertion sorting method.
INPUT:
OUTPUT:
16
Engr. Muhammad Arslan Rafique
Lab Assessment
Understanding Ability to Generation and Ability to
of Engineering perform Interpretation of relate
Tools experiment using results using experiment Total
(Criteria 1) Engineering Tool modern tools with theory and 15
2 (Dev C++) (Dev C++) its significance Mark
(Criteria 2) (Criteria 3) (Criteria 4) s
6 4 3
Task 1
Task 2
Task 3
Task 4
Task 5
Task 6
Averag
e Marks
17
Engr. Muhammad Arslan Rafique