Labels

Showing posts with label Sorting. Show all posts
Showing posts with label Sorting. Show all posts

Heap Sort

// C++ program for implementation of Heap Sort
#include <iostream>
using namespace std;

// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
void heapify(int arr[], int n, int i)
{

    int largest = i;  // Initialize largest as root
    int l = 2*i + 1;  // left = 2*i + 1
    int r = 2*i + 2;  // right = 2*i + 2

    // If left child is larger than root
    if (l < n && arr[l] > arr[largest])
        largest = l;
       
    // If right child is larger than largest so far
    if (r < n && arr[r] > arr[largest])
        largest = r;

    // If largest is not root
    if (largest!=i)
    {
   
    swap(arr[i], arr[largest]);
    //which ever is large left or right from ROOT will be swap by ROOT
    //a[i] is root,a[largest] is left or right node value.
   

        // Recursively heapify the affected sub-tree
        heapify(arr, n, largest);
    }
}

// main function to build max heap
void heapSort(int arr[], int n)
{
    // Build heap (rearrange array)
    for (int i = n/2-1; i >= 0; i--)
    {
        heapify(arr, n, i);
       
    }
   

    // One by one extract an element from heap
    for(int k=n-1;k>=0;k--)
    {
        // Move Root  Node to end
        swap(arr[0], arr[k]);
        // call max heapify on the reduced heap
        heapify(arr, k, 0);
    }
}

/* A utility function to print array of size n */
void printArray(int arr[], int n)
{
    for (int i=0; i<n; ++i)
        cout << arr[i] << " ";
    cout << "\n";
}

// Driver program
int main()
{
    int arr[] = {50,75,60,35,45,25,95};
    int n = sizeof(arr)/sizeof(arr[0]); 
    heapSort(arr, n);

    cout << "Sorted array is \n";
    printArray(arr, n);
}

Selection Sort

//Code

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
void selectionSort(int *p,int n)
{
int min_index;
for(int i=0;i<n-1;i++)
//last value ke liye loop nh chalana hai kyuki vo end mai 
//correct position pai aa jaegi
{  min_index=i;
for(int j=i+1;j<n;j++)
//har value ko baki n-1 values se comapre krega.
{
if(p[j]<p[min_index])
{
swap(p[min_index],p[j]);
}


}
}
}
main()
{

int a[]={5,4,7,8,2,1,9,10,6,3};
int n=sizeof(a)/sizeof(a[0]);
selectionSort(a,n);
printf("Sorted Array\n");
for(int i=0;i<n;i++)
{
printf("%d ",a[i]);
}

}
--------------------------------------------------------------------------------------------------------------------------
/*Alternatively you can use this method for user input

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
void selectionSort(int *p,int n)
{
int min_index;
for(int i=0;i<n-1;i++)
//last value ke liye loop nh chalana hai kyuki vo end mai 
//correct position pai aa jaegi
{  min_index=i;
for(int j=i+1;j<n;j++)
//har value ko baki n-1 values se comapre krega.
{
if(p[j]<p[min_index])
{
swap(p[min_index],p[j]);
}


}
}
}
main()
{
int n;
   printf("Enter Size of Array\n");
   scanf("%d",&n);
   printf("Enter the %d elements of array\n",n);
int *arr;
arr=(int*)malloc(100*sizeof(int));
//we can use either calloc or malloc.Syntax of calloc is
//arr=(int*)calloc(n,sizeof(int)); here you save the memory
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
selectionSort(arr,n);
printf("Sorted Array\n");
for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}


}*/

Quick Sort Code

//Quick Sort

#include<stdio.h>
void quick_sort(int*,int,int);
int a[5],i;
main()
{
 
 printf("Enter Five values\n\n");
 for(i=0;i<5;i++)
 {
  scanf("%d",&a[i]);
 }
 quick_sort(a,0,4);
 printf("\nFinal Sorted values\n\n");
 for(i=0;i<5;i++)
 {
  printf("%d",a[i]);
 }
 return 0;
}
void quick_sort(int*p,int first,int last)
{
 int low,high,temp,pivot;
 if(first<last){
 low=first;high=last;pivot=first;
 while(low<high)
 {
      while(p[low]<=p[pivot])
        low++;
      while(p[high]>p[pivot])
         high--;
         if(low<high)
        {
      
        temp=p[low];
        p[low]=p[high];
        p[high]=temp;
    
        }
        
    }
         temp=p[pivot];
         p[pivot]=p[high];
         p[high]=temp;
        quick_sort(p,first,high-1);
        quick_sort(p,high+1,last);
         
    }
}

Insertioin Sort Code

//Insertion Sort

#include<stdio.h>
int a[100],n,i;
main()
{
void insertionSort(int*);
printf("Enter Size of Array\n");
scanf("%d",&n);
printf("Enter Array Elements\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
insertionSort(a);
printf("Sorted Array\n");
for(i=0;i<5;i++)
{
printf("%d ",a[i]);
}
}
void insertionSort(int*a)
{
int i,j,temp;
for(i=0;i<n;i++)
{
temp=a[i];
for(j=i-1;j>=0&&temp<a[j];j--)
{
a[j+1]=a[j];
}
a[j+1]=temp;
}
}

Bubble Sort Code

//BubbleSort.


#include<stdio.h>
int a[100],i,n;
main()
{
void bubbleSort(int*);
        printf("Enter Size of Array\n");
       scanf("%d",&n);
printf("Enter Array Elements Of Array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubbleSort(a);
printf("Sorted Array\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}
void bubbleSort(int*a)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j+1]<a[j])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
}