0% found this document useful (0 votes)
36 views5 pages

DSA Sorting

The document contains code implementations of bubble sort, insertion sort, min heap sort, and max heap sort on an integer array. It provides the output of: [1] the array after the second pass of bubble sort and the number of swaps, [2] the array after the fourth pass of insertion sort, and [3] the sorted arrays using min heap sort and max heap sort algorithms.

Uploaded by

Ravil Patel
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)
36 views5 pages

DSA Sorting

The document contains code implementations of bubble sort, insertion sort, min heap sort, and max heap sort on an integer array. It provides the output of: [1] the array after the second pass of bubble sort and the number of swaps, [2] the array after the fourth pass of insertion sort, and [3] the sorted arrays using min heap sort and max heap sort algorithms.

Uploaded by

Ravil Patel
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/ 5

Assignment – DSA –

Submitted by – Ravil Patel

Roll No. – 211020437

Branch – DSAI

Given Data: 45,36,54,27,63,72,61 and 18

1.Implement Bubble sort and print the result of Pass 2 and number of swaps in Pass 2

Code:

#include <iostream>
using namespace std;
int main()
{
    int arr[8] = {45, 36, 54, 27, 63, 72, 61, 18};
    int swap = 0;
    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 8 - i - 1; j++)
        {
            if (arr[j] > arr[j + 1])
            {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                if (i == 1)
                {
                    swap++;
                }
            }
        }
    }
    cout << "Array after 2 swaps" << endl;

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


    {
        cout << arr[i] << " ";
    }
    cout<<endl;
    cout << "Number of swaps in the second swap is: " << swap << endl;
    return 0;
}

Output:

2. Implement Insertion sort and print the result of Pass 4.

Code:

#include <iostream>
using namespace std;

void insertionSort(int array[], int size) {


  for (int step = 1; step < 4; step++) {
    int key = array[step];
    int j = step - 1;
    while (key < array[j] && j >= 0) {
      array[j + 1] = array[j];
      --j;
    }
    array[j + 1] = key;
  }
}

int main() {
  int data[] = {45,36,54,27,63,72,61,18};
  int size = sizeof(data) / sizeof(data[0]);
  insertionSort(data, size);
  cout << "Array after Pass 4 is :\n";
  for (int i = 0; i < size; i++) {
    cout << data[i] << " ";
  }
  cout << endl;
}

Output:

3. Print Max Heap sort and Min Heap sort


MINHEAP Sort of the given array

Code:

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

void heapify(int arr[], int n, int i)


{
    int smallest = i;
    int l = 2 * i + 1;
    int r = 2 * i + 2;

    if (l < n && arr[l] < arr[smallest])


        smallest = l;

    if (r < n && arr[r] < arr[smallest])


        smallest = r;

    if (smallest != i)
    {
        swap(arr[i], arr[smallest]);

        heapify(arr, n, smallest);
    }
}

void heapSort(int arr[], int n)


{
    for (int i = n / 2 - 1; i >= 0; i--)
        heapify(arr, n, i);

    for (int i = n - 1; i >= 0; i--)


    {
        swap(arr[0], arr[i]);

        heapify(arr, i, 0);
    }
}

int main()
{
    int arr[] = {45,36,54,27,63,72,61, 18};
    int n = sizeof(arr) / sizeof(arr[0]);
    heapSort(arr, n);
    cout << "Sorted array using minHeap is:  "<<endl;
    for(auto i:arr){
        cout<<i<<" ";
    }
}

Output:

MaxHeap

Code:

#include <iostream>  
using namespace std;  
void heapify(int a[], int n, int i)  
{  
    int largest = i;
    int left = 2 * i + 1;  
    int right = 2 * i + 2;  
 
    if (left < n && a[left] > a[largest])  
        largest = left;  

    if (right < n && a[right] > a[largest])  


        largest = right;  
    if (largest != i) {  
        int temp = a[i];  
        a[i] = a[largest];  
        a[largest] = temp;  
         
        heapify(a, n, largest);  
    }  
}  

void heapSort(int a[], int n)  


{  
 
    for (int i = n / 2 - 1; i >= 0; i--)  
        heapify(a, n, i);  
    for (int i = n - 1; i >= 0; i--) {  
        int temp = a[0];  
        a[0] = a[i];  
        a[i] = temp;  
         
        heapify(a, i, 0);  
    }  
}  

void printArr(int a[], int n)  


{  
    for (int i = 0; i < n; ++i)  
    {  
        cout<<a[i]<<" ";  
    }  
         
}  
int main()  
{  
    int a[] = {45,36,54,27,63,72,61,18};  
    int n = sizeof(a) / sizeof(a[0]);  
    heapSort(a, n);  
    cout<<"Array After sorting is - \n";
    for(auto i:a){
        cout<<i<<" ";
    }
    return 0;  
}  

Output:

You might also like