-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicSorting.java
More file actions
112 lines (92 loc) · 2.75 KB
/
basicSorting.java
File metadata and controls
112 lines (92 loc) · 2.75 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.util.Arrays; //->For Inbiult Sort
import java.util.Collections; // ->For reverse order (used for reverse Inbuilt Sorting)
import java.util.*;
public class basicSorting {
public static void print(int A[]) {
for(int i = 0 ; i < A.length ; i++){
System.out.print(A[i] + " ");
}
}
// public static void bubbleSort(int []A){
// for(int i = 0 ; i < A.length - 1; i++){
// for(int j = 0; j < A.length - 1; j++){
// if(A[j] > A[j+1]){
// int temp = A[j];
// A[j] = A[j+1];
// A[j+1] = temp;
// }
// }
// }
// }
// public static void selectionSort(int a[]){
// int n = a.length;
// for(int i = 0;i<n-1;i++){
// int min = i ;
// for(int j = i+1 ; j < n ; j++){
// if(a[j] < a[min]){
// min = j;
// }
// }
// int temp = a[min];
// a[min] = a[i] ;
// a[i] = temp;
// }
// }
// public static void insertionSort(int arr[]){
// int n = arr.length;
// for(int i = 1 ; i < n ; i++ ){
// int curr = arr[i];
// int prev = i - 1;
// while( prev >= 0 && (curr > arr[prev]) ){
// arr[prev+1] = arr[prev];
// prev--;
// }
// arr[prev + 1] = curr;
// }
// }
public static void countingSort(int arr[]) {
int largest = Integer.MIN_VALUE;
for(int i = 0 ;i < arr.length ; i++ ){
largest = Math.max(largest,arr[i]);
}
int count[] = new int[largest + 1];
for(int i = 0 ;i <arr.length ; i++){
count[arr[i]]++;
}
int update = 0;
for(int i = 0 ; i < count.length ; i++){
while(count[i] > 0){
arr[update] = i;
update++;
count[i]--;
}
}
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
// //**** Taking Array Size Input **** //
System.out.println("Enter the Size of the Array");
int n = sc.nextInt();
// //**** Taking Array Input **** //
System.out.println("Enter the Elements of the Array");
int arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = sc.nextInt();
}
// bubbleSort(arr);
// print(arr);
// selectionSort(arr);
// print(arr);
// insertionSort(arr);
// print(arr);
// // InBuilt Sort
// Arrays.sort(arr); //-> Ascending order
// Arrays.sort(arr,0, 3); //-> Only Selected Part
// Integer A[] = {5,4,1,3,2};
// Arrays.sort(A,Collections.reverseOrder()); //->Reverse Order
// Arrays.sort(A,0,3,Collections.reverseOrder()); //-> Only Selected Part
// print(A);
// countingSort(arr);
// print(arr);
}
}