RSS

Category Archives: Sorting in Arrays

PROGRAM TO CHECK THE TYPE OF SORTING IN AN ARRAY (2ND LOGIC)

//THIS PROGRAM CHECKS WHETHER AN ARRAY IS SORTED OR NOT. IF SORTED THEN IT CHECKS THE TYPE OF SORT - ASCENDING OR DESCENDING

#include <iostream>

using namespace std;

char checksort(int arr[], int size);

int main()
{
 int size;
 cout<<"\nEnter the size of the array \n";
 cin>>size;

 int *arr = new int[size];

 cout<<"\nInput the array\n";

  for(int i = 0; i<size; i++)
  {
   cin>>arr[i];
  }

char s = checksort(arr,size);

if(s == 'A')
 cout<<"\nAscending...\n";
else if(s == 'D')
 cout<<"\nDescending...\n";
else
 cout<<"\nNot ordered...\n";

return 0;
}

 


char checksort(int arr[], int size)
 {
 char sort = 'N';
 int flag = 0;
//check ascending
 for(int i = 0; i<size-1; i++)
 {
 if(arr[i]>arr[i+1])
 {
 flag = 1;
 break;
 }
 }

if(flag == 0)
 {
 return ('A');
 }

//check descending

flag = 0;

for(int j = 0; j<size-1; j++)
 {
 if(arr[j]<arr[j+1])
 {
 flag = 1;
 break;
 }
 }

if(flag == 0)
 {
 return ('D');
 }

return 'N';
 }

Advertisements
 

INSERTION SORT: DESCENDING ORDER (BETTER LOGIC)

// THIS PROGRAM SORTS AN ARRAY IN DESCENDING ORDER USING INSERTION SORT

#include <iostream>

using namespace std;

void insertionsort(int arr[], int size);

int main()
{
 int arr[100], size;
 cout<<"Enter the size of the array \n";
 cin>>size;

 cout<<"\nInput the array \n";

 for(int i = 1;i<=size; i++)
 {
  cin>>arr[i];
 }

 arr[0] = 32767;

 insertionsort(arr,size);

 cout<<"\nThe sorted array is \n";

 for(int i = 1; i<=size; i++)
 {
  cout<<arr[i]<<' ';
 }

return 0;
}

void insertionsort(int arr[], int size)
{
 for(int j = 2; j<=size; j++)
 {
  int key = arr[j];
  int k = j-1;

  while(key>arr[k])
  {
   arr[k+1] = arr[k];
   k--;
  }

  arr[k+1] = key;
 }
}
 

COUNTING SORT: ASCENDING ORDER

//PROGRAM TO SORT AN ARRAY IN ASCENDING ORDER USING COUNTING SORT

#include <iostream>
#include <cstring>

using namespace std;

void countingsort(int arr[], int size, int);

int main()
{
	int size, upper;
	
	cout<<"\nEnter the size of the array \n";
	cin>>size;
	cout<<"\nEnter the upper limit\n";
	cin>>upper;
	
	int *arr = new int[size+10];
	
	cout<<"\nInput the array \n";
	
		for(int i = 0; i<size; i++)
		{
			cin>>arr[i];
		}
		
	countingsort(arr,size, upper);
	
	cout<<"\nThe sorted array is \n";
	
		for(int i = 0; i<size; i++)
		{
			cout<<arr[i]<<' ';
		}
		
	return 0;
}

void countingsort(int arr[], int size,int upper)
{
	int *count = new int[upper+10];
	
	int *output = new int[size+10];
		  
	memset(count, 0, (upper+10)*sizeof(int));
		
	for(int i = 0; i<size; i++)
	{
		count[arr[i]]++;
	}
	
	for(int i = 1; i<=upper; i++)
	{
		count[i] += count[i-1];
	}
	
	for(int i = 0; i<size; i++)
	{
		output[count[arr[i]]-1] = arr[i];
		count[arr[i]]--;
	}
	
	for(int i = 0; i<size; i++)
	{
		arr[i] = output[i];
	}  
}
 

PROGRAM TO SORT AN INTEGER ARRAY IN DESCENDING ORDER USING COUNTING SORT

//THIS PROGRAM USES COUNTING SORT TO SORT AN ARRAY OF INTEGERS IN DESCENDING ORDER

#include <iostream>
#include <cstring>

using namespace std;

void countingsort(int [],int size, int upper);

int main()
{
	int size;
	
	cout<<"\nEnter the size of the array \n";
	cin>>size;
	
	int *arr = new int[size+10];
	
	int upper;
	
	cout<<"\nEnter the upper limit\n";
	cin>>upper;
	
	cout<<"\nEnter the array \n";
	
		for(int i= 0; i<size; i++)
		{
			cin>>arr[i];
		}
		
	countingsort(arr,size,upper);
	
	cout<<"\nThe sorted  array is \n";
	
	for(int i = 0; i<size; i++)
	cout<<arr[i]<<' ';
	
	return 0;
}

void countingsort(int arr[], int size, int upper)
{
	int *output = new int[size+10];
	int *count = new int[upper+10];
	
	for(int i = 0; i<=upper; i++)
	{
		count[i] = 0;
	}
	
	for(int i = 0; i<size; i++)                         
	{
	count[arr[i]]++;                                
	}                                 
									      
	for(int i = upper-1; i>=0; i--)       
	{
	count[i] = count[i] + count[i+1];
	}
	
	for(int i = 0; i<size; i++)
	{
		output[count[arr[i]] - 1] = arr[i];
		count[arr[i]]--;
	}
	
	for(int i = 0; i<size; i++)
	{
		arr[i] = output[i];
	}
}
 

PROGRAM TO SORT AN ARRAY IN ASCENDING ORDER USING SELECTION SORT

// THIS PROGRAM SORTS AN ARRAY IN ASCENDING ORDER USING SELECTION SORT

#include <iostream>

using namespace std;

void selectionsort(int arr[], int);

int main()
{
int size;
cout<<“\nEnter the size of the array \n”;
cin>>size;

int *arr = new int[size];

cout<<“\nInput the array \n”;

for(int i =0; i<size; i++)
cin>>arr[i];

selectionsort(arr, size);

cout<<“\nThe sorted array is \n”;

for(int i =0; i<size; i++)
cout<<arr[i]<<‘ ‘;

return 0;
}

void selectionsort(int arr[], int size)
{
int temp, min, pos;

for(int i = 0; i<size; i++)
{
temp = arr[i];
min = arr[i];

for(int j = i+1; j<size; j++)
{
if(arr[j] < min)
{
min = arr[j];
pos = j;
}
}
arr[i] = arr[pos];
arr[pos] = temp;
}
}

 
 

PROGRAM TO SORT AN ARRAY IN ASCENDING ORDER USING BUBBLE SORT

// THIS PROGRAM SORTS AN ARRAY IN ASCENDING ORDER USING BUBBLE SORT

#include <iostream>

void bubblesort(int arr[], int);

using namespace std;

int main()
{
int size;
cout<<“\nEnter the size of the array \n”;
cin>>size;

int *arr = new int[size];

cout<<“\nInput the array \n”;

for(int i = 0; i<size; i++)
{
cin>>arr[i];
}

bubblesort(arr,size);

cout<<“\nThe sorted array is \n”;

for(int i =0; i<size; i++)
cout<<arr[i]<<‘ ‘;

return 0;
}

void bubblesort(int arr[], int size)
{
int flag = 0;
int pass = 0;
int temp = -1;
do
{
flag = 0;
pass++;
for(int i = 0; i<size-pass; i++)
{
if(arr[i] > arr[i+1])
{
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
flag = 1;
}
}
}
while(flag == 1);
}

 
 

PROGRAM TO MERGE TWO ARRAYS USING MERGE SORT (RESULTANT IS IN DESCENDING ORDER)

/* THIS PROGRAM MERGES TWO ARRAYS SORTED IN ASCENDING ORDER USING MERGE SORT SUCH THAT THE
RESULTANT ARRAY IS SORTED IN DESCENDING ORDER */

#include <iostream.h>
#include <conio.h>

int main()
{
clrscr();
int arr1[100], arr2[100], res[200], size1, size2;
cout<<“Enter the size of the first array \n”;
cin>>size1;
cout<<“\nEnter the size of the second array \n”;
cin>>size2;
cout<<“\nInput first array \n”;
for(int i =0; i<size1; i++)
cin>>arr1[i];
cout<<“\nInput second array \n”;
for(i =0; i<size2; i++)
cin>>arr2[i];
int p1 = size1-1, p2 = size2-1;
i = 0;
for(; i<size1+size2; i++)
{
if(p1>= 0 && p2 >= 0)
{
if(arr1[p1]>arr2[p2])
{
res[i] = arr1[p1];
p1–;
}
else
{
res[i] = arr2[p2];
p2–;
}
}
else
break;
}

if(i != size1+size2)
{
if(p2<0)
{
while(i<size1+size2)
{
res[i] = arr1[p1];
p1–;
i++;
}
}
else
{
if(p1<0)
{
while(i<size1+size2)
{
res[i] = arr2[p2];
p2–;
i++;
}
}
}
}

cout<<“\nDisplaying the resultant array \n”;

for(i =0; i<size1+size2; i++)
cout<<res[i]<<‘ ‘;
getch();
return 0;
}