Sort 1 to N by swapping adjacent elements


An array is a linear data structure that stores the elements and a sorted array contains all the elements in increasing order. Sorting an array by swapping the adjacent elements means we can swap the adjacent elements any number of times and we have to sort the array. We will be given two array’s first array is the array to be sorted and another array is a Boolean array that represents whether the current element is swappable or not.

If the given array is of the length N then all the elements present will be from 1 to N.

Input

Given array:   1 2 4 3 5 7 6 
Boolean array: 0 1 1 1 0 1 1 

Output

Yes, we can sort the array. 

Explanation

We can swap 3 and 4, as they are swappable and we can swap 6 and 7.

Input

Given array:   1 2 4 3 5 7 6 
Boolean array: 0 1 1 1 0 1 0 

Output

No, we cannot sort the array. 

Explanation

We can swap 3 and 4, as they are swappable, but we cannot swap 6 and 7 makes this array unsorted.

Naive Approach

In this approach, we will traverse over the array, and we will traverse over the Boolean array. If the current index is swappable then we will traverse to the last swappable index and sort all the elements by using the sort function. At last, we will check if the array is sorted or not.

Example

#include <bits/stdc++.h>
using namespace std;
// function to check if the current array is sorted or not 
bool isSorted(int arr[], int n){    
   // traversing over the array 
   for(int i =1; i<n ;i++){
      if(arr[i] < arr[i-1]){
         return false;
      }
   }
   // traversed over the whole array means sorted 
   return true;
}
// function to swap the elements of the array 
bool check(int arr[], bool brr[], int n){
	// traversing over the boolean array 
	for (int i = 0; i < n - 1; i++) {
		if (brr[i] == 1){
			int j = i;
			while (brr[j] == 1){
			   j++;
			}			
			// using sort function sort the array 
			sort(arr + i, arr + 1 + j);
			i = j; // updating the value of i
		}
	}

	return isSorted(arr, n);
}
int main(){
	int arr[] = { 1, 4, 2, 3, 5, 7, 6};
	bool brr[] = { 0, 1, 1, 1, 0, 1, 1 };
	int n = sizeof(arr) / sizeof(arr[0]); // getting the size of array    
	if(check(arr, brr, n)){
	   cout<<"Yes, the given array can be sorted"<<endl;
	}
	else{
	   cout<<"No, the given array cannot be sorted"<<endl;
	}    
   return 0;
}

Output

No, the given array cannot be sorted

Time and Space Complexity

The time complexity of the above code is O(N*log(N)) where N is the size of the given array.

The space complexity of the above code is O(1), as we are not using any extra space.

Approach 2

In this approach, we will traverse over the given array and will check if the current element is not at its sorted place and non-swappable then we will return false. Otherwise, we will get the data of all the elements which are swappable for the current point to the next unswappable index or the end of the array. If the elements present in that range are less than the current range or more than we will return false otherwise at the end we can return true.

Example

#include <bits/stdc++.h>
using namespace std;
// function to check the array can be sorted by swapping 
bool check(int arr[], bool brr[], int n){    
   int notAtPlace = 0; // variable to store elements that are not at place 
   int swappable = 0; // variable to store total elements that are swappable    
	// traversing over the Boolean array 
	for (int i = 0; i < n ; i++) {
	   if(arr[i] != (i+1) && brr[i] == 0) return false;	    
		if (brr[i] == 1 && arr[i] != i+1){
			int j = i;			
			//variable to get the maximum swappable value 
			// variable to get the minimum swappable value
			int mx = arr[i]-1;
			int mi = arr[i]-1;			
			while(j < n && brr[i] == 1){
			   mx = max(mx, arr[j]-1);
			   mi = min(mi, arr[j]-1);
			   j++;
			}
			if(mx > j || mi < i){
			   return false;
			}
			i = j-1;
		}
	}
   // traversed over the whole array means possible to sort
	return true;
}
int main(){
	int arr[] = { 1, 4, 2, 3, 5, 7, 6};
	bool brr[] = { 0, 1, 1, 1, 0, 1, 1 };
	int n = sizeof(arr) / sizeof(arr[0]); // getting the size of array     
	if(check(arr, brr, n)){
	   cout<<"Yes, the given array can be sorted"<<endl;
	}
	else{
	   cout<<"No, the given array cannot be sorted"<<endl;
	}    
   return 0;
}

Output

Yes, the given array can be sorted

Time and Space Complexity

The time complexity of the above code is O(N) where N is the size of the given array, as we are just traversing over the array just once.

The space complexity of the above code is O(1), as we are not using any extra space.

Conclusion

In this tutorial, we have implemented a program to find whether the given array can be sorted or not by swapping the adjacent elements which are swappable. Any array is swappable or not we will be provided by the another array. We have implemented two approaches one with the O(N*log(N)) time and O(1) space complexity while the other with the same space complexity but improved time complexity of O(N).

Updated on: 01-Sep-2023

147 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements