Skip to content

Commit

Permalink
Updated AD Lab Assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
SOMNATH0904 committed Jan 18, 2024
1 parent 7c99a6c commit da614c1
Showing 1 changed file with 32 additions and 57 deletions.
89 changes: 32 additions & 57 deletions Third_Semester/Algorithm-Design-1(AD1)/LAB-04(Sorting)/Q3.java
Original file line number Diff line number Diff line change
@@ -1,71 +1,46 @@
// Ques 3 : Check reverse

import java.util.Scanner;
import java.util.Arrays;

public class Q3 {

public static void print(int[] arr) {
int n = arr.length;
for(int i = 0; i < n; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}

public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

public static void reverse(int[] arr, int i, int j) {
while(i < j) {
swap(arr, i, j);
i++;
j--;
class Q3 {
public static boolean checkReverse(int arr[], int n) {
int temp[] = new int[n];
for (int i = 0; i < n; i++) {
temp[i] = arr[i];
}
}

public static boolean sortedCheck(int[] arr) {
int flag = 0;
for(int i = 1; i < arr.length; i++) {
if(arr[i-1] > arr[i]) {
flag = 1;
Arrays.sort(temp);
int front;
for(front = 0; front < n; front++) {
if(temp[front] != arr[front]) {
break;
}
return false;
}
return true;
}
public static void reverseSortedCheck(int[] arr) {
for(int i = 0; i < arr.length; i++) {
for(int j = 0; j < arr.length; j++) {
reverse(arr, i, j);
boolean res = sortedCheck(arr);
if(res == true) {
System.out.println("Reversing the Subarray : ");
reverse(arr, i, j);
}
for(int k = i; k <= j; k++) {
System.out.println(arr[k] + " ");
System.out.println("Makes the Array Sorted!!");
}
reverse(arr, i, j);
int back;
for(back = n-1; back >= 0; back--) {
if(temp[back] != arr[back]) {
break;
}
}
System.out.println("There is no SubArray whose reversal makes the array sorted");
if(front >= back) {
return true;
}
do{
front++;
if (arr[front - 1] < arr[front]) {
return false;
}
} while(front != back);
return true;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Size of array : ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter Array Elements : ");
for(int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
int arr[] = {1, 2, 5, 4, 3};
int n = arr.length;
System.out.print("Is the Array Sorted : ");
if (checkReverse(arr, n)) {
System.out.print("True");
} else {
System.out.print("False");
}
System.out.println("Array Elements are : ");
print(arr);
reverseSortedCheck(arr);
}
}

0 comments on commit da614c1

Please sign in to comment.