Sorting Algorithms Implementation: Bubble Sort, Selection Sort, and Insertion Sort, Lab Reports of Data Structures and Algorithms

Solution Manual of lab no 2 of data structure and Algorithm

Typology: Lab Reports

2020/2021

Available from 01/15/2022

syed-afaq-hussain-shah
syed-afaq-hussain-shah 🇺🇸

14 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
18-EE-21
Lab # 02
Title:
To implement bubble sort, selection sort and insertion sort algorithms on
an array type data structure
Objective:
To perform different sorting method on an array data type structure
To implement bubble sort, selection sort and insertion sort algorithms on
an array type data structure
Equipment Required:
PC/ Laptop
Dev C++ software
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:
Bubble Sort
Selection Sort
Insertion Sort
Bubble Sort Algorithm:
Bubble Sort is a simple algorithm which is used to sort a given set of n elements provided in form of an
array with n number of elements. Bubble Sort compares the entire element one by one and sort them
based on their values.
If the given array has to be sorted in ascending order, then bubble sort will start by comparing the first
element of the array with the second element, if the first element is greater than the second element, it
will swap both the elements, and then move on to compare the second and the third element, and so on.
If we have total n elements, then we need to repeat this process for n-1 times.
It is known as bubble sort, because with every complete iteration the largest element in the given array,
bubbles up towards the last place or the highest index, just like a water bubble rises up to the water
surface.
Sorting takes place by stepping through all the elements one-by-one and comparing it with the adjacent
element and swapping them if required.
Implementing Bubble Sort Algorithm:
Following are the steps involved in bubble sort (for sorting a given array in ascending
order):
Starting with the first element (index = 0), compare the current element with the next
element of the array.
If the current element is greater than the next element of the array, swap them.
If the current element is less than the next element, move to the next element. Repeat Step 1.
Let's consider an array with values {5, 1, 6, 2, 4, 3}
Below, we have a pictorial representation of how bubble sort will sort the given array
1 | P a g e
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Sorting Algorithms Implementation: Bubble Sort, Selection Sort, and Insertion Sort and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

Lab # 02

Title:

To implement bubble sort, selection sort and insertion sort algorithms on

an array type data structure

Objective:  To perform different sorting method on an array data type structure  To implement bubble sort, selection sort and insertion sort algorithms on an array type data structure  Equipment Required:  PC/ Laptop  Dev C++ software  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:  Bubble Sort  Selection Sort  Insertion Sort  Bubble Sort Algorithm: Bubble Sort is a simple algorithm which is used to sort a given set of n elements provided in form of an array with n number of elements. Bubble Sort compares the entire element one by one and sort them based on their values. If the given array has to be sorted in ascending order, then bubble sort will start by comparing the first element of the array with the second element, if the first element is greater than the second element, it will swap both the elements, and then move on to compare the second and the third element, and so on. If we have total n elements, then we need to repeat this process for n-1 times. It is known as bubble sort , because with every complete iteration the largest element in the given array, bubbles up towards the last place or the highest index, just like a water bubble rises up to the water surface. Sorting takes place by stepping through all the elements one-by-one and comparing it with the adjacent element and swapping them if required.

 Implementing Bubble Sort Algorithm:

Following are the steps involved in bubble sort (for sorting a given array in ascending

order):

 Starting with the first element (index = 0), compare the current element with the next element of the array.  If the current element is greater than the next element of the array, swap them.  If the current element is less than the next element, move to the next element. Repeat Step 1. Let's consider an array with values {5, 1, 6, 2, 4, 3} Below, we have a pictorial representation of how bubble sort will sort the given array

So as we can see in the representation above, after the first iteration, 6 is placed at the last index, which is the correct position for it. Similarly after the second iteration, 5 will be at the second last index, and so on  Selection Sort Algorithm: Selection sort is conceptually the simplest sorting algorithm. This algorithm will first find the smallest element in the array and swap it with the element in the first position, then it will find the second smallest element and swap it with the element in the second position, and it will keep on doing this until the entire array is sorted. It is called selection sort because it repeatedly selects the next-smallest element and swaps it into the right place.

 How Selection Sort Works?

Following are the steps involved in selection sort (for sorting a given array in ascending order):  Starting from the first element, we search the smallest element in the array, and replace it with the element in the first position.  We then move on to the second position, and look for smallest element present in the subarray, starting from index 1, till the last index.  We replace the element at the second position in the original array, or we can say at the first position in the subarray, with the second smallest element.  This is repeated, until the array is completely sorted.

 Then, we make the third element of the array as key and will compare it with elements to its left and insert it at the right position.  And we go on repeating this, until the array is sorted. Let's consider an array with values {5, 1, 6, 2, 4, 3} Below, we have a pictorial representation of how bubble sort will sort the given array.

LAB TASKS

Task # 1: Write a program to sort the following list by using bubble sorting method.

 Program code:

#include using namespace std; void swapping(int &a, int &b) { //swap the content of a and b int temp; temp = a; a = b; b = temp;} void display(int *array, int size) { for(int i = 0; i<size; i++) cout << array[i] << " "; cout << endl;} void bubbleSort(int *array, int size) { for(int i = 0; i<size; i++) { int swaps = 0; //flag to detect any swap is there or not for(int j = 0; j<size-i-1; j++) { if(array[j] > array[j+1]) { //when the current item is bigger than next swapping(array[j], array[j+1]); swaps = 1; //set swap flag }} if(!swaps) break; // No swap in this pass, so array is sorted }} int main() { int n; cout<<"\n\n\t>>>>>>>> Bubble Sort Method <<<<<<<<\n"; cout<<"\n----------------------------------------------------"; cout <<"\n\n\tEnter the number of elements: "; cin >> n; cout<<"\n----------------------------------------------------\n"; int arr[n]; //create an array with given number of elements for(int i = 0; i<n; i++) { cout<<"\n\t\tEnter "<<i+1<<" element: "; cin>>arr[i];} cout<<"\n----------------------------------------------------\n"; cout << "\tArray before Sorting: "; display(arr, n); cout<<"\n----------------------------------------------------\n"; bubbleSort(arr, n); cout << "\tArray after Sorting: "; display(arr, n); cout<<"\n----------------------------------------------------\n"; }

} } //Bubble sort function void bubblesort() { for( int i=1;i<n ;i++ )//for n-1 passes { for( int j=0; j<n-1; j++) { if(arr[j] > arr[j+1]) { string temp; temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } void display() { cout<<endl; cout<<"\n----------------------------------------------------\n"; cout<<"\t\tSorted array elements \n"; cout<<"\n----------------------------------------------------\n"; for( int j=0; j<n; j++) cout<<"\n\t\t\t"<<arr[j]<<endl; }}; int main(){ bubble list; list.read(); list.bubblesort(); list.display(); return 0;}

 Program Output:

Task # 3: Write a program to sort the following list by using selection sorting method.

 Program Code:

#include using namespace std; void swapping(int &a, int &b) { int temp; temp = a; a = b; b = temp;} void display(int *array, int size) { for(int i = 0; i<size; i++) cout << array[i] << " "; cout << endl;} void selectionSort(int *array, int size) { int i, j, imin; for(i = 0; i<size-1; i++) { imin = i; //get index of minimum data for(j = i+1; j<size; j++) if(array[j] < array[imin]) imin = j; swap(array[i], array[imin]);} } int main() { int n; cout<<"\n\n\t>>>>>>>> Selection Sort Method <<<<<<<<\n"; cout<<"\n--------------------------------------------------------"; cout <<"\n\n\tEnter the number of elements: "; cin>>n; cout<<"\n----------------------------------------------------\n"; int arr[n]; //create an array with given number of elements for(int i=0;i<n;i++) { cout<<"\n\t\tEnter "<<i+1<<" element: "; cin>>arr[i];} cout<<"\n----------------------------------------------------\n"; cout << "\tArray before Sorting: "; display(arr, n); cout<<"\n----------------------------------------------------\n"; selectionSort(arr, n); cout << "\tArray after Sorting : "; display(arr, n); cout<<"\n----------------------------------------------------\n"; }

for(int i = 0; i < two.length(); i++) {b = two.at(i); putchar (toupper(b)); two.at(i) = b;} if(one.compare(two) > 0) {cout << one << endl; return 1;} else {cout << two << endl; return 0; } } int main() { int Size; cout<<"\n\n\t>>>>>>>> Selection Sort Method <<<<<<<<\n"; cout<<"\n--------------------------------------------------------"; cout <<"\n\n\tEnter the number of elements: "; cin >> Size; cout<<"\n----------------------------------------------------\n"; string input[Size]; for(int i =0; i < Size; i++) { cout<<"\n\t\tEnter "<<i+1<<" element: "; cin >> input[i]; } cout<<"\n----------------------------------------------------\n"; cout<<"\t\tSorted array elements \n"; cout<<"\n----------------------------------------------------\n"; selectionSort(input, Size); return 0; }

 Program Output:

Task # 5: Write a program to sort the following list by using insertion sorting method.

 Program Code:

#include

using namespace std;

void display(int *array, int size) {

for(int i = 0; i<size; i++)

cout << array[i] << " ";

cout << endl;}

void insertionSort(int *array, int size) {

int key, j;

for(int i = 1; i<size; i++) {

key = array[i];//take value

j = i;

while(j > 0 && array[j-1]>key) {

array[j] = array[j-1];

j--;}

array[j] = key;}} //insert in right place

int main() {

int n;

cout<<"\n\n\t>>>>>>>> Insertion Sort Method <<<<<<<<\n";

cout<<"\n--------------------------------------------------------";

cout <<"\n\n\tEnter the number of elements: ";

cin >> n;

cout<<"\n--------------------------------------------------------\n";

int arr[n]; //create an array with given number of elements

for(int i = 0; i<n; i++) {

cout<<"\n\t\tEnter "<<i+1<<" element: ";

cin >> arr[i]; }

cout<<"\n--------------------------------------------------------\n";

cout << "\tArray before Sorting: ";

display(arr, n);

insertionSort(arr, n);

cout<<"\n--------------------------------------------------------\n";

cout << "\tArray after Sorting : ";

display(arr, n);

cout<<"\n--------------------------------------------------------\n";}

{if(strcmp(b[i],k)>0) { i--; if(i==-1) f=0;} else f=0;} p=j; while(p>i+1) {strcpy(b[p],b[p-1]); p--;} strcpy(b[i+1],k);} cout<<"\n--------------------------------------------------------\n"; cout<<"\t\tSorted array elements \n"; cout<<"\n--------------------------------------------------------\n"; for(int i =0; i <n; i++) {cout<<"\n\t\t\t"<<b[i]<<endl;} cout<<"\n--------------------------------------------------------\n"; return 0;}

 Program Output: