Checking if an array is sorted?

Joined
Aug 29, 2003
Messages
2,874
Reaction score
2
Hello again.

I'm curious if there is a way to determine if an array of integers is sorted or not? What I want is a piece of code that scans the integers that are inputted by the user, and then check to see whether or not they are in order, without actually sorting the numbers.

Here's what I've got so far.

Code:
#include <stdio.h>
#include <ctype.h>
#define TRUE = 1
#define FALSE = 0
void InputList(int array[50], int &num);
int IsSorted(int array[50], int n);
main()
{
	int a[10], num;
	InputList(a, num);
	IsSorted(a, num);
}
void InputList(int array[50], int &num)
{
	int i;
	printf("Enter the number of values in the list => ");
	scanf("%d", &num);
	for(i=0; i < num; ++i)
	{
		printf("Enter the data item => ");
		scanf("%d", &array[i]);
	}
}
int IsSorted(int array[], int n)
{
	int i;
	printf("Items in the original array\n");
	for(i = 0; i < n; ++i) {
		printf("%4d\n", array[i]);
	}
	for(i = 0; i < n; ++i) {
		for(i = 0; i < n - 1; ++i) {
			if (array[i] > array[i + 1]){
				printf("Array is not sorted\n");
			}
			else
			{
				printf("Array is sorted\n");
			}
		}
	}
	return 0;
}

IsSorted is the function that I am currently working on. It's down to the point where the information is printed out on the screen, however, I can't figure out how to only get the "Array is sorted"/"Array is not sorted" to print only once. And inserting breaks didn't work because it only tests the first two numbers. I'm also weak with for loops.

Who wants to guide me to the solution to my problem? :cheese:
 
Dunno. I haven't tested this myself (and I'm a tad bit of a sloppy coder, so don't trust it), but logically it seems this piece of code would work:
Code:
void isSorted(int[] a; int n) {
	int i;
	for (i=1;i<n;++i) {
		if (a[i]<a[i-1]) {
			break;
		}
	}
	if (i-1<n) {
		for (i=1;i<n;++i) {
			if (a[i]>a[i-1]) {
				break;
			}
		}
		if (i-1<n) {
			printf("Array is not sorted\n");
		} else {
			printf("Array is sorted (descending)\n");
		}
	} else {
		printf("Array is sorted (ascending)\n");
	}
}
I haven't analyzed your code yet, so I don't know what's with yours. I just thought I'd try the problem. they look similar superficially. :\

PS y r your for loops nested? I don't get what your trying to do with that. And it seems that you only check to see if it's sorted in one direction. Is that your intent?
 
Nicely done Phisionary. It worked. I just needed to add a return value, and change the a to array in your code to match mine. But above all else, I understand what I was doing wrong.

You've saved me much time. :)

EDIT: Scratch that; it's just printing "the array is not sorted" everytime. :O
 
your most welcome. I think I might have mixed up the ascending/descending though. that ( a<a-1 ) fail-test thing confused me ;)

btw, why do you need a return value on a function that doesn't return an informative value? just curious..
 
I'm not sure either, probably because it's an integer type instead of a void.

...still trying to solve the new problem.
 
well, I'm just saying that there's no reason to have a return like that on a function (that I'm aware of) unless it's main().

Yeah. it doesn't work. Bah. I'll get back to you if I figure it out. :)
 
alright, working code. I think the only real change is it's i, not i-1 in the if comparisons. boy, i realize now it's pretty ugly. not self-evident, uh,.. coding, or whatever :laugh:
added some comments and stuff
Code:
#include <stdio.h>
void isSorted(int a[], int n) {
	int i;
	for (int k=0; k<n; k++) {
		printf(" a[%i] = %i\n",k,a[k]);
	}
	for (i=1;i<n;++i) {		//from the second index to the end
		if (a[i]<a[i-1]) {	//does the array fail descending order?
			break;	//if so, break.
		}
	}
	if ( i < n ) {				//if having failed descending order
		for (i=1;i<n;++i) {		//from the second index to the end
			if (a[i]>a[i-1]) {	//does the array fail ascending order?
				break;	//if so, break
			}
		}
		if ( i < n ) {						//if having failed ascending oreder test
			printf("Array is not sorted\n");		// <-that
		} else {						//else (failed descending, passed ascending)
			printf("Array is sorted (descending)\n");	// <-that
		}
	} else {							//else (passed descending)
		printf("Array is sorted (ascending)\n");	// <-that
	}
}

int main() {
	int array1[] = {1,3,5,7,9,11,13,15,17,50};
	int array2[] = {12,11,9,7,5,7,9,12,13,13};
	int array3[] = {24,24,20,16,13,12,10,6,3,-1};
	printf("--array1-- \n");
	isSorted(array1,10);
	printf("--array2-- \n");
	isSorted(array2,10);
	printf("--array3-- \n");
	isSorted(array3,10);
	return 0;
}
 
aren't your answers the wrong way round?

Shouldn't it be:
Code:
		if ( i < n ) {						//if having failed ascending oreder test
			printf("Array is not sorted\n");		// <-that
		} else {						//else (failed descending, passed ascending)
			printf("Array is sorted (ascending)\n");	// <-that
		}
	} else {							//else (passed descending)
		printf("Array is sorted (descending)\n");	// <-that
	}
?
 
Back
Top