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

C/C++ program to Find that a Given Data Array In Sorted Or Not

Leave a Comment
/*The Following program 1st asks the array size & then the user will input the Array elements, the program hop by hop check for the condition a[i]<a[i-1] if it is true then it will display a success message other wise the array is not  sorted*/

/*To use this program in Turbo C++, you need to convert this in ANSI standard,  which can be easily done by removing 'using namespace std;' & instead of iostream  we should have to write iostream.h*/

#include <iostream>    //Using the new C++ Standard
#include <stdlib.h>      //For system("pause") function
using namespace std;  //Using standard Namespace 
void main()
{
float* myArray;
char* message = 0;
unsigned int arraySize = 0;
cout<<"Enter Array Size : ";


cin>>arraySize;        
//Getting the array size

myArray = new float[arraySize];  //Defining new float array with the entered size

cout<<"\nInput array elements: (Add 'Space' as a separator): ";
for (unsigned int i = 0; i< arraySize; i++)
{
cin>>myArray[i];     
//inputting the array elements
}

for(unsigned int i = 1; i< arraySize; i++)
if(myArray[i] < myArray[i-1])
      //Condition For Increasing sorted
message = "\nThe list is not sorted!\n";

/*For Decreasing sort checking use myArray[i] > myArray[i-1] in place of
myArray[i] < myArray[i-1] */

if(!message)
message = "\nThe list is sorted!\n";


delete [] myArray;
cout<<message;
system("pause");
}
   //End of the program


ScreenShots:
ScreenShot of RunTime of C/C++ program to Find that a Given Data Array In Sorted Or Not
ScreenShot of RunTime of C/C++ program to Find that a Given Data Array In Sorted Or Not

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...