How to "reverse" selection sorting? - java

So here I have a selection sort to order values from lowest to highest, how would I change that so it orders the values from highest to lowest?
int min;
for (int i = 0; i < array.length; i++) {
// Assume first element is min
min = i;
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[min]) {
min = j;
}
}
if (min != i) {
final int temp = array[i];
array[i] = array[min];
array[min] = temp;
}
itsATextArea.append(array[i] + "\n");
}

You just need to change the signs in the code
if (array[j] > array[max])
{
//assign it here
max = j;
}
following is how the code looks after the modification.
int max;
for (int i = 0; i < array.Length; i++)
{
// Assume first element is max
max = i;
for (int j = i + 1; j < array.Length; j++)
{
if (array[j] > array[max])
{
max = j;
}
}
if (max != i)
{
int temp = array[i];
array[i] = array[max];
array[max] = temp;
}
}

How about:
java.util.Arrays.sort(array);
java.util.Arrays.reverse(array);

Related

Java sorting algorithm problems

I need to make a programm in java with the selection sort algorithm. So i tried this to do that but the code doesn't works.
Problem with this code is that it doesn't swap numbers. Instead, it replaces array[i] with the minimum number found. You can modify your loop like this to do the swapping.
for (int i = 0; i < array.length; i++) {
int minIndex = i;
for (int j = i; j < array.length; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
if (array[minIndex] != array[i]) {
int wert = array[minIndex];
array[minIndex] = array[i];
array[i] = wert;
}
}
For selection sort use this method
public static void selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int index = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[index]) {
index = j;//searching for lowest index
}
}
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}
If you need an ascend order just use:
Arrays.sort(array) from java.util library
But if you need to sort descending I'd suggested to reffer to:
https://www.baeldung.com/java-sorting-arrays

Selection sort in descending order - JAVA

I have a selection sort implemented to sort an array of random integers. I would like the user to choose between ascending or descending order. The ascending sort works flawlessly, although descending does not. Here is what my selection sort looks like:
public String selection(int[] array,int num,String order) {
String output = "";
int min;
// This is the descending selection sort
if (order == "desc") {
for (int i = num - 1; i >= 0; i--) {
// Assume first element is min
min = i;
for (int j = i + 1; j < num; j++) {
if (array[j] < array[min]) {
min = j;
}
}
if (min != i) {
final int temp = array[i];
array[i] = array[min];
array[min] = temp;
}
output = output + Integer.toString(array[i]) + "\n";
}
} // This is the ascending selection sort
else {
for (int i = 0; i < num; i++) {
// Assume first element is min
min = i;
for (int j = i + 1; j < num; j++) {
if (array[j] < array[min]) {
min = j;
}
}
if (min != i) {
final int temp = array[i];
array[i] = array[min];
array[min] = temp;
}
output = output + Integer.toString(array[i]) + "\n";
}
}
return(output.trim());
}
I've seen a few questions similar to mine, although none of the questions I saw had their selection sort set up like this so I was unable to implement their solutions.
Firstly, the if blocks should compare to minPosition and maxPosition, not i. Secondly, if you are selecting both minimum and maximum, then your inner for loop should stop at a.length - i, not a.length (since the top i elements are also sorted). Doing both gives you this as the ascending order algorithm.
public static void SortAscending(int[] a){
for(int i = 0; i < a.length; i++){
int maxPosition=i;
int minPosition=i;
for(int j = i+1; j < a.length - i; j++){
if(a[j] < a[minPosition]){
minPosition = j;
}
if(a[j] > a[maxPosition]){
maxPosition = j;
}
}
/*
if(i < a.length/2-1)
*/
swap(a,maxPosition,i);
swap(a,minPosition,a.length-i-1);
}
}
To switch to descending order, simply add one line.
public static void SortDescending(int[] a){
for(int i = 0; i < a.length; i++){
int maxPosition=i;
int minPosition=i;
for(int j = i+1; j < a.length - i; j++){
if(a[j] < a[minPosition]){
minPosition = j;
}
if(a[j] > a[maxPosition]){
maxPosition = j;
}
}
/*
if(i < a.length/2-1)
*/
swap(a,minPosition,maxPosition); // <-- this line
swap(a,maxPosition,i);
swap(a,minPosition,a.length-i-1);
}
}
Use swap function https://www.geeksforgeeks.org/collections-swap-method-in-java-with-examples/

How to structure an array of multiple outputs in one array (Java)

My code basically runs through an array, finds the maximum, and then locates the indices correlated with it. The issue I have is that I would like the indices in one array instead of multiple.
For example (actual output):
[1] [2] [3] [4]
Wanted output: [1,2,3,4]
Here is my code:
int index;
int num = myArray[0];
for (int i = 0; i < myArray.length; i++) {
if (num < myArray[i]) {
num = myArray[i];
}
}
for (int j = 0; j < myArray.length; j++) {
if(num == myArray[j]){
index = j;
System.out.println("[" + j + "]");
}
}
I don't know how to do this without redoing all the code. Thanks.
int index;
int num = myArray[0];
for (int i = 0; i < myArray.length; i++) {
if (num < myArray[i]) {
num = myArray[i];
}
}
System.out.print("[");
int count = 0;
for (int j = 0; j < myArray.length; j++) {
if(num == myArray[j]){
index = j;
if(count++ > 0)
System.out.print(",");
System.out.print(j);
}
}
System.out.println("]");
int maxValue = myArray[0];
int countMaxValues = 1;
for (int i = 1; i < myArray.length; i++) {
if (myArray[i] > maxValue) {
maxValue = myArray[i];
countMaxValues = 1;
}
else if (myArray[i] == maxValue) {
countMaxValues++;
}
}
int maxValueIndices = new int[countMaxValues];
int i = 0;
for (int j = 0; j < myArray.length; j++) {
if(myArray[j] == maxValue){
maxValueIndices[i++] = j;
}
}
System.out.println(Arrays.toString(maxValueIndices));

Please clarify me thats wrong in my selection sort code

In this method only one element is getting sorted rest of the elements are are not sorted.
Please help me to find where the actual problem is
private static int[] selectSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int min_Ele = i;
for (int j = 1; j < n; j++) {
if (arr[j] <= arr[min_Ele]) {
min_Ele = j;
}
}
if (i != min_Ele) {
int tmp = arr[i];
arr[i] = arr[i = min_Ele];
arr[min_Ele] = tmp;
}
}
return arr;
}
Your mistake is that your inner loop should start from i+1 and not from 1.
public static int[] selectSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int min_Ele = i;
for (int j = i+1; j < n; j++) {
if (arr[j] <= arr[min_Ele]) {
min_Ele = j;
}
}
if (i != min_Ele){
//swap
int tmp = arr[i];
arr[i] = arr[i = min_Ele];
arr[min_Ele] = tmp;
}
return arr;
}
private static int[] selectSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n-1; i++) {
int min_Ele = i;
for (int j = i+1; j < n; j++) {
if (arr[j] <= arr[min_Ele]) {
min_Ele = j;
}
}
if (i != min_Ele) {
int tmp = arr[i];
arr[i] = arr[min_Ele];
arr[min_Ele] = tmp;
}
}
return arr;
}

Java - Selection Sort - only switching once

So the first switch definitely occurs between the lowest value 3, and 5, but it doesn't keep going after that. This makes me think there is something wrong with one of the for loops?
public class SelectionSort
{
public static void main (String[] args)
{
int [] list;
list = new int[5];
list[0] = 4;
list[1] = 5;
list[2] = 12;
list[3] = 9;
list[4] = 3;
for (int i = 0; i < list.length-1; ++i) {
int index = i;
for (int j = 1; j < list.length; ++j) {
if (list[j] < list[index]) {
int temp = list[j];
list[j] = list[index];
list[index] = temp;
}
}
}
for (int k = 0; k < list.length; ++k) {
System.out.print(list[k] + ", ");
}
}
}
Short versiĆ³n:
for (int i = 0; i < list.length-1; i++)
for (int j = i+1; j < list.length; j++)
if (list[j] < list[i]) {
int temp = list[j];
list[j] = list[i];
list[i] = temp;
}
for (int k = 0; k < list.length; k++) {
System.out.print(list[k] + ", ");
}
after if (list[j] < list[index]) {, you have to update index if boolean statement gets satisfied so you need to index = j and do the swap after
see follwing :
public class MySelectionSort {
public static int[] doSelectionSort(int[] arr){
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[index])
index = j;
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
return arr;
}
for (int i = 0; i < list.length; ++i) {
int index = i;
for (int j = i + 1; j < list.length; ++j) {
if (list[j] < list[index]) {
index = j; }}
if (index != i) {
int temp = list[i];
list[i] = list[index];
list[index] = i; }}
To cut down on the number of swaps, the loop on j should only be looking the best item to swap with item i. There should be at most one swap for each value of i. (That's what makes it a Selection sort. If you do multiple swaps for each i, you might as well be doing Bubble Sort.
But that's for efficiency. The reason your code isn't working is that you start the loop on j at j = 1 instead of j = i + 1.

Categories

Resources