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

C++ Program Code To Perform Quick Sort of array elements entered by user

Leave a Comment
/*The following C++ Program code performs the Quick Sorting Algorithm on an array which is being entered by the user. The function part() is user to partition the array as per the pivot value & function quick() performs the Quick Sort. The user is asked to enter the elements, which is then stored in array & displayed. Program performs quick sorting  displays the sorted result.*/

#include<iostream>
#include<conio.h>
using namespace std;

//Function for partitioning array
int part(int low,int high,int *a)
{
     int i,h=high,l=low,p,t; 
//p==pivot
     p=a[low];
     while(low<high)
     {
                    while(a[l]<p)
                    {
                                   l++;
                    }
                    while(a[h]>p)
                    {
                                   h--;
                    }
                    if(l<h)
                    {
                                t=a[l];
                                a[l]=a[h];
                                a[h]=t;
                    }
                    else
                    {
                        t=p;
                        p=a[l];
                        a[l]=t;
                        break;
                    }
     }
     return h;   
}

void quick(int l,int h,int *a)
{
  int index,i;
  if(l<h)
  {
          index=part(l,h,a);
          quick(l,index-1,a);
          quick(index+1,h,a);
  }
}

int main()
{
      int a[100],n,l,h,i;
      cout<<"Enter number of elements:";
      cin>>n;
      cout<<"Enter the elements (Use Space As A Separator):";
      for(i=0;i<n;i++)
      cin>>a[i];
      cout<<"\nInitial Array:\n";
      for(i=0;i<n;i++)
      {
                      cout<<a[i]<<"\t";
      }  
      h=n-1;
      l=0;
      quick(l,h,a);
      cout<<"\nAfter Sorting:\n";
      for(i=0;i<n;i++)
      {
                cout<<a[i]<<"\t";
      }
      getch();
      return 0;
}


Screenshot:
C++ Program Code To Perform Quick Sort of array elements entered by user

Read More...

Get the name, age and address of students who enrolled for admission of a certificate course and order them based on their age by finding the pivot.

Leave a Comment
quick sort program in c



#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct student{
char name[30];
int age;
char add[90];
}a[100];


void q_sort(struct student a[],int low,int top)
{
int i;
if(top>low)
{
i=split(a,low,top);
q_sort(a,low,i-1);
q_sort(a,i+1,top);
}
}


int split(struct student a[],int low,int top)
{
int i,p,q;
struct student t;
p=low+1;
q=top;
i=a[low].age;
while(q>=p)
{
while(a[p].age p++;
while(a[q].age>i)
q--;
if(q>p)
{
t=a[p];
a[p]=a[q];
a[q]=t;
}
}
t=a[low];
a[low]=a[q];
a[q]=t;
return q;
}

int main()
{
int n,i;
printf("\nEnter the max. limit:");
scanf("%d",&n);
for(i=0;i {
printf("\nEnter the name of the student:");
fflush(stdin);
gets(a[i].name);
printf("\nEnter the age of the student:");
scanf("%d",&a[i].age);
printf("\nEnter the address:");
fflush(stdin);
gets(a[i].add);
}

q_sort(a,0,n-1);
printf("\nAfter sorting!\n");
for(i=0;i {
printf("\nThe student name:%s",a[i].name);
printf("\nAge of the student:%d",a[i].age);
printf("\nThe address:%s",a[i].add);
}

getche();
return 0;
}
Read More...