0% found this document useful (0 votes)
54 views11 pages

Practical No:1: Implementation of Different Sorting Techniques. 1) Write A Program To Implement Bubble Sort Program

The document describes programs to implement various sorting algorithms: 1) Bubble sort, insertion sort, selection sort, shell sort, and radix sort. 2) For each algorithm, the program code is provided to implement the sorting technique on sample data. 3) The output of each program is printed to show the sorted data.

Uploaded by

Tushar Koli
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)
54 views11 pages

Practical No:1: Implementation of Different Sorting Techniques. 1) Write A Program To Implement Bubble Sort Program

The document describes programs to implement various sorting algorithms: 1) Bubble sort, insertion sort, selection sort, shell sort, and radix sort. 2) For each algorithm, the program code is provided to implement the sorting technique on sample data. 3) The output of each program is printed to show the sorted data.

Uploaded by

Tushar Koli
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/ 11

DS Practical

Roll no:29

Practical No:1

Implementation of different sorting techniques.


1] Write a program to implement Bubble Sort
Program:
#include <bits/stdc++.h>
using namespace std;

void swap(int *xp, int *yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void bubbleSort(int arr[], int n)


{
int i, j;
for (i = 0; i < n-1; i++)

for (j = 0; j < n-i-1; j++)


if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}

void printArray(int arr[], int size)


{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
DS Practical
Roll no:29

int main()
{
int arr[] = { 91, 44, 53, 63, 43, 12, 58 };
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
cout<<"Sorted array: \n";
printArray(arr, n);
return 0;
}

OUTPUT:
DS Practical
Roll no:29

2] Write a program to implement Insertion Sort.


Program:
#include <bits/stdc++.h>
using namespace std;

void insertionSort(int arr[], int n)


{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;

while (j >= 0 && arr[j] > key)


{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = { 2, 111, 1, 50, 699 };
int n = sizeof(arr) / sizeof(arr[0]);

insertionSort(arr, n);
printArray(arr, n);

return 0;
DS Practical
Roll no:29

OUTPUT:
DS Practical
Roll no:29

3] Write a program to implement Selection Sort.


Program:
#include <bits/stdc++.h>
using namespace std;

void swap(int *xp, int *yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void selectionSort(int arr[], int n)


{
int i, j, min_idx;
for (i = 0; i < n-1; i++)
{
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(&arr[min_idx], &arr[i]);
}
}
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
cout << "Sorted array: \n";
printArray(arr, n);
DS Practical
Roll no:29

return 0;
}

OUTPUT:
DS Practical
Roll no:29

4] Write a program to implement Shell Sort


Program:
#include <iostream>
using namespace std;

int shellSort(int arr[], int n)


{

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;
}
}
return 0;
}

void printArray(int arr[], int n)


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

int main()
{
int arr[] = {1, 4, 77, 2, 3}, i;
int n = sizeof(arr)/sizeof(arr[0]);

cout << "Array before sorting: \n";


printArray(arr, n);
DS Practical
Roll no:29

shellSort(arr, n);

cout << "\nArray after sorting: \n";


printArray(arr, n);

return 0;
}

OUTPUT:
DS Practical
Roll no:29

5] Write a program to implement Radix Sort


Program:
#include <iostream>

using namespace std;

// Get maximum value from array.


int getMax(int arr[], int n)
{
int max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}

// Count sort of arr[].


void countSort(int arr[], int n, int exp)
{
// Count[i] array will be counting the number of array values having that 'i'
digit at their (exp)th place.
int output[n], i, count[10] = {0};

// Count the number of times each digit occurred at (exp)th place in every
input.
for (i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;

// Calculating their cumulative count.


for (i = 1; i < 10; i++)
count[i] += count[i-1];

// Inserting values according to the digit '(arr[i] / exp) % 10' fetched into
count[(arr[i] / exp) % 10].
for (i = n - 1; i >= 0; i--)
{
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
DS Practical
Roll no:29

// Assigning the result to the arr pointer of main().


for (i = 0; i < n; i++)
arr[i] = output[i];
}

// Sort arr[] of size n using Radix Sort.


void radixsort(int arr[], int n)
{
int exp, m;
m = getMax(arr, n);

// Calling countSort() for digit at (exp)th place in every input.


for (exp = 1; m/exp > 0; exp *= 10)
countSort(arr, n, exp);
}

int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;

int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}

radixsort(arr, n);

// Printing the sorted data.


cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
return 0;
}
DS Practical
Roll no:29

OUTPUT:

You might also like