-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortAnArrayUsingRecursion.java
More file actions
69 lines (67 loc) · 2.84 KB
/
Copy pathSortAnArrayUsingRecursion.java
File metadata and controls
69 lines (67 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package RecursionAndBacktracking;
/*
* We have given an array of integers ans we have to sort the array but by using Recursion only.
*/
import java.util.ArrayList;
import java.util.Scanner;
public class SortAnArrayUsingRecursion {
// Method for sorting the array
public static void sortArray(ArrayList<Integer> arrayList){
// Base condition
if(arrayList.size() == 1){
return;
}
int temp = arrayList.get(arrayList.size()-1); // storing last element
arrayList.remove(arrayList.size()-1); // remove last element from the arraylist
sortArray(arrayList); // now call sortArray(arrayList> -> now size is n-1(previous size - 1)
insertTemp(arrayList, temp); // call met hod for storing last element into it's appropriate position
}
// method for storing last element into it's proper position
// for ascending order sorting
public static void insertTemp(ArrayList<Integer> arrayList, int temp){
// Base Condition
if(arrayList.size()==0 || arrayList.get(arrayList.size()-1)<=temp){
arrayList.add(temp);
return;
}
// if the last element is not smaller than temp
// then store last value into some variable names val and remove that element from the arrayList
// now call insertTemp and at last add val
int val = arrayList.get(arrayList.size()-1);
arrayList.remove(arrayList.size()-1);
insertTemp(arrayList, temp);
arrayList.add(val);
}
// for descending order sorting
// public static void insertTemp(ArrayList<Integer> arrayList, int temp){
// // Base Condition
// if(arrayList.size()==0 || arrayList.get(arrayList.size()-1)>=temp){
// arrayList.add(temp);
// return;
// }
// // if the last element is not smaller than temp
// // then store last value into some variable names val and remove that element from the arrayList
// // now call insertTemp and at last add val
// int val = arrayList.get(arrayList.size()-1);
// arrayList.remove(arrayList.size()-1);
// insertTemp(arrayList, temp);
// arrayList.add(val);
// }
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of test cases:");
int t = sc.nextInt();
while (t-- > 0) {
System.out.println("Enter elements: if want to stop enter -1");
ArrayList<Integer> arrayList = new ArrayList<>();
int ele = 0;
ele = sc.nextInt();
while (ele != -1){
arrayList.add(ele);
ele = sc.nextInt();
}
sortArray(arrayList);
System.out.println(arrayList);
}
}
}