forked from Arunvijay28/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomSort3524.java
89 lines (79 loc) · 2.92 KB
/
randomSort3524.java
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Random;
import java.util.Scanner;
public class randomSort3524 {
public static void main(String[] args) {
System.out.println("2021503524 - Mugundh J B");
System.out.println("Date: " + LocalDate.now() + " Time: " + LocalTime.now());
Scanner scanner = new Scanner(System.in);
System.out.print("Enter no of elements: ");
int n = scanner.nextInt();
int[] arr = new int[n];
arr = RandomArray(arr);
int comparisonsInt = OrderNSort(arr.clone());
char[] charArray = CharArray(arr);
int comparisonsChar = OrderNSort(charArray.clone());
System.out.println("Number of comparisons for integer array sorting: " + comparisonsInt);
System.out.println("Number of comparisons for character array sorting: " + comparisonsChar);
}
public static int[] RandomArray(int[] arr) {
Random random = new Random();
for (int i = 0; i < arr.length; i++) {
arr[i] = random.nextInt(26);
}
return arr;
}
public static char[] CharArray(int[] intArray) {
char[] charArray = new char[intArray.length];
for (int i = 0; i < intArray.length; i++) {
charArray[i] = (char) (intArray[i] + 'a');
}
return charArray;
}
public static int OrderNSort(int[] arr) {
int comparisons = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
comparisons++;
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println("Sorted array in ascending order(Selection Sort):");
printArray(arr);
return comparisons;
}
public static int OrderNSort(char[] arr) {
int comparisons = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
comparisons++;
if (arr[j] > arr[j + 1]) {
char temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println("\nSorted array in ascending order(Selection Sort):");
printArray(arr);
return comparisons;
}
public static void printArray(int[] arr) {
System.out.print("|");
for (int i = 0; i < arr.length; i++) {
System.out.printf(" %d |", arr[i]);
}
}
public static void printArray(char[] arr) {
System.out.print("|");
for (int i = 0; i < arr.length; i++) {
System.out.printf(" %c |", arr[i]);
}
System.out.print("\n");
}
}