0% found this document useful (0 votes)
72 views6 pages

DS Lab 3

The document contains code for implementing different sorting algorithms in C++ including bubble sort, insertion sort, shell sort, and comb sort. It defines functions to sort arrays using each algorithm, display the sorted array, and includes a main function that tests each algorithm by sorting sample arrays and printing the results. The code takes user input for the shell sort and comb sort algorithms to specify the size of the array and populate it with elements to be sorted.

Uploaded by

Evil Sight
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views6 pages

DS Lab 3

The document contains code for implementing different sorting algorithms in C++ including bubble sort, insertion sort, shell sort, and comb sort. It defines functions to sort arrays using each algorithm, display the sorted array, and includes a main function that tests each algorithm by sorting sample arrays and printing the results. The code takes user input for the shell sort and comb sort algorithms to specify the size of the array and populate it with elements to be sorted.

Uploaded by

Evil Sight
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

DS lab 3

Abdul Rafiu
22k-4162
BCS-3B
Task 4:
#include <cstdlib>
#include <iostream>
using namespace std;

void displayarray(int arr[]){


int n = 10;
for(int i = 0; i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}

void BubbleSort(int arr[]){


int n=10;
int numofcomparision=0;
for(int i = 0; i< n; i++)
{
for(int j=i+1;j<n;j++){
if(arr[j]<arr[i]){
int temp = arr[j];
arr[j]=arr[i];
arr[i]=temp;
numofcomparision++;
}
}
}
cout<<"Sorted with bubble sort by "<< numofcomparision << " number of
comparisions"<<endl;
displayarray(arr);
}

int main(){
int arr[] = {10,2,0,14,43,25,18,1,5,45};
BubbleSort(arr);
}
Task 5:
#include <cstdlib>
#include <iostream>
using namespace std;

void displayarray(int arr[]){


int n = 7;
for(int i = 0; i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}

void InsertionSort(int arr[]){


int n=7;
for (int i = 1; i<n; i++){
int key = arr[i];
int j=i-1;
while(j>=0 && arr[j]>key){
arr[j+1]=arr[j];
j--;
}
arr[j+1]=key;
}
displayarray(arr);
}

int main(){
int arr[] = {9,5,1,4,3,14,119};
InsertionSort(arr);
}

Task 6:
#include <iostream>

using namespace std;

void displayarray(int arr[],int n){


for(int i = 0; i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}

void shellSort(int arr[], int n) {


int numofcomparision=0;
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i += 1) {
int temp = arr[i];
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = temp;
}
}
cout<<"Sorted with Shell sort: "<<endl;
displayarray(arr,n);
}

int main(){
int n;
cout<<"Enter number of elements in array: ";
cin>>n;
int arr[n];

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


{
cout<<"Enter element "<<i+1<<" in array: ";
cin>>arr[i];
}

shellSort(arr,n);

}
Task 7:
#include <iostream>

using namespace std;

void displayarray(int arr[], int n)


{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

void CombSort(int arr[], int n)


{
int step = n, j, k;
bool swapped = true;

while ((step = int(step / 1.3)) > 1)


{
swapped = false;

for (int j = n - 1; j >= step; j--)


{
k = j - step;

if (arr[j] < arr[k])


{
int temp = arr[j];
arr[j] = arr[k];
arr[k] = temp;
swapped = true;
}
}
for (int i = 0; i < n - 1 && swapped; i++)
{
for (int j = n - 1; j > i; j--)
{
if (arr[j] < arr[j - 1])
{
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
swapped = true;
}
}
}

if (!swapped)
break;
}

cout << "Sorted with Comb sort: " << endl;


displayarray(arr, n);
}

int main()
{
int n;
cout << "Enter number of elements in array: ";
cin >> n;
int arr[n];

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


{
cout << "Enter element " << i + 1 << " in array: ";
cin >> arr[i];
}

CombSort(arr, n);
}

You might also like