







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Solution Manual of lab no 2 of data structure and Algorithm
Typology: Lab Reports
1 / 13
This page cannot be seen from the preview
Don't miss anything!








Title:
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.
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.
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.
Task # 1: Write a program to sort the following list by using bubble sorting method.
#include
} } //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;}
#include
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; }
{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;}