import java.util.Arrays;
//Bubble sort
public class Example4 {
public static void main(String[] args) {
int arr[] = { 10, 40, 4, 60, 5, 1 };
// 10,4,40,5,1,60
// bubbleSort(arr);
selectionSortMax(arr);
System.out.println(Arrays.toString(arr));
}
public static void selectionSort(int[] input) {
for (int i = 0; i < input.length ; i++) {
int minIndex = i;
for (int j = i + 1; j < input.length; j++) {
if (input[j] < input[minIndex]) {
minIndex = j;
}
}
int temp = input[minIndex];
input[minIndex] = input[i];
input[i] = temp;
}
}
// Need to write code with maximum to minimum
//Bubble sort
public class Example4 {
public static void main(String[] args) {
int arr[] = { 10, 40, 4, 60, 5, 1 };
// 10,4,40,5,1,60
// bubbleSort(arr);
selectionSortMax(arr);
System.out.println(Arrays.toString(arr));
}
public static void selectionSort(int[] input) {
for (int i = 0; i < input.length ; i++) {
int minIndex = i;
for (int j = i + 1; j < input.length; j++) {
if (input[j] < input[minIndex]) {
minIndex = j;
}
}
int temp = input[minIndex];
input[minIndex] = input[i];
input[i] = temp;
}
}
// Need to write code with maximum to minimum
public static void selectionSortMax(int[] input) {
for (int i = 0; i < input.length ; i++) {
int maxIndex = input.length-1-i;
for (int j = input.length-2-i; j >= 0; j--) {
if (input[j] > input[maxIndex]) {
maxIndex = j;
}
}
int temp = input[maxIndex];
input[maxIndex] = input[input.length-1-i];
input[input.length-1-i] = temp;
}
}
//10, 40, 4, 60, 5, 1
}
No comments:
Post a Comment