RSS

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

29 Mar
//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
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: