Operations in a Sorted Array
Inserting in a sorted array, checking if an array is sorted and shifting negative elements to the left of array.
Last updated
int isSorted(int a[], int n) {
// Iterate till length - 1
for(int i = 0; i < n-1; i++) {
// Case for unsorted array
if (a[i] > a[i+1]) {
return 0;
}
}
return 1;
}void rearrange(int a[], int n) {
int j = 0;
for (int i = 0; i < n; i++) {
// If negative number => Swap a[i] and a[j]
if (a[i] < 0) {
if (i != j) {
swap(&a[i], &a[j]);
}
j++;
}
}
}