Showing posts with label quick sort. Show all posts
Showing posts with label quick sort. Show all posts

Wednesday, 29 November 2017

Implementation of Quick Sort in C/C++

// Created By Mohd Mujtaba... Visit https://playwithcplus.blogspot.in fb-> //facebook.com/playwithcplus

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

int part(int arr[],int low,int high)
{
int i=(low-1);
int pivot=arr[high];
for(int j=low;j<high;j++)
{
if(arr[j]<=pivot)
{
i++;
swap(arr[i],arr[j]);
}
}
swap(arr[i+1],arr[high]);
return(i+1);
}
void quicksort(int arr[],int low,int high)
{
if(low<high)
{
int pi=part(arr,low,high);
quicksort(arr,low,pi-1);
quicksort(arr,pi+1,high);
}
}
main()
{
int arr[] = {10, 7, 8, 9, 1, 5};
    int n = sizeof(arr)/sizeof(arr[0]);
    quicksort(arr, 0, n-1);
    printf("Sorted array:");
    for(int i=0;i<n;i++)
    cout<<" "<< arr[i];
}

Friday, 10 November 2017

Program to Implement Insertion In Heap in C/C++

// Insertion In Heap Tree Data Structure

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

int arr[10],n;

void insertHeap(int num,int loc)
{


int par;
while(loc>0)
{
par=(loc-1)/2;
if(num<=arr[par])
{
arr[loc]=num;
return;
}

arr[loc]=arr[par];
loc=par;
}

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


main()
{
n=0;

insertHeap(50,n);
n+=1;
insertHeap(30,n);
n+=1;
insertHeap(40,n);
n+=1;
insertHeap(29,n);
n+=1;
insertHeap(55,n);
n+=1;
cout<<"Insertion In HEAP: ";
disp(n);


}



Amazon1Ads