1

So I made this selection sort code, but I want it to increment one loop of the sort each time the function is called.

So I thought okay. Why not just remove the outer for loop and replace index with a static variable up top and increment it each time the function finishes its operations. But that just messed up the sort really badly. Can someone help?

Question: How do I go through the sort one step at a time each time the function is called?

I don't want it to sort the entire thing all at once

private static void selectionSort(int[] array) {
    for (int index = 0; index < array.length; index++) {
        int currentMin = array[index];
        int indexOfMin = index;

        for(int j = index+1; j < array.length; j++) {
            if(array[j] < currentMin) { 
                currentMin = array[j];
                indexOfMin = j;
            }
        }
        swap(array, index, indexOfMin);
    }
}

private static void swap(int[] a, int i, int j) {
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}
3
  • What's your question? What's wrong with this function? Commented Dec 14, 2015 at 5:01
  • Saw your edits. Do you mean you wanna print out every step of the program like 312 -> 132 -> 123? Commented Dec 14, 2015 at 5:05
  • Basically I'm showing this sort visually with bars, and each time I click the button it will step through the sort and adjust the bars Commented Dec 14, 2015 at 5:06

3 Answers 3

1

Why wouldn't global variable do the job? Do you wanna post your solution and we can take a look? Or you can also just pass the starting index to the function and call it with incremental index every time the button is pressed.

private static void selectionSort(int fromIndex, int[] array) {
    int currentMin = array[fromIndex];
    int indexOfMin = fromIndex;

    for(int j = fromIndex+1; j < array.length; j++) {
        if(array[j] < currentMin) {
            currentMin = array[j];
            indexOfMin = j;
        }
    }
    swap(array, fromIndex, indexOfMin);
}

private static void swap(int[] a, int i, int j) {
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}

Then in main, call it like this:

for (int fromIndex = 0; fromIndex < array.length; fromIndex++) {
    // Stop here and wait for button click.
    selectionSort(fromIndex, array);
}
Sign up to request clarification or add additional context in comments.

1 Comment

found out why it didnt work initially... I was incrementing the index in the wrong place lol. face palm
0

call function

Collections.sort(array, new SortData());

in SortData.java

import java.util.Comparator;

import tg.cw.pd.model.ModelcurrentMin;

public class SortData implements Comparator<Object>
{
    public int compare(Object o1, Object o2) 
    {

        int result;

        String desc1 = ((ModelcurrentMin)o1).getcurrentMin();
        String desc2 = ((ModelcurrentMin)o2).getcurrentMin();

        result = desc1.compareTo(desc2);    //asc
        return   result;
   }
}

Comments

0

Check this one..

public static void main(String[] args) {
    int[] array = new int[]{3, 4, 2, 7, 1, 5, 6};
    System.out.println(Arrays.toString(array));   //[2, 4, 3, 7, 5, 1, 6]

    selectionSort(array);                         
    System.out.println(Arrays.toString(array));   //[1, 4, 3, 7, 5, 2, 6]

    selectionSort(array);                         
    System.out.println(Arrays.toString(array));   //[1, 2, 3, 7, 5, 4, 6]

    selectionSort(array);                        
    System.out.println(Arrays.toString(array));   //[1, 2, 3, 4, 5, 7, 6]

    selectionSort(array);                      
    System.out.println(Arrays.toString(array));   //[1, 2, 3, 4, 5, 6, 7]
}

private static void selectionSort(int[] array) {
    for (int index = 0; index < array.length; index++) {
        int currentMin = array[index];
        int indexOfMin = index;
        boolean swapNeeded = false;

        for (int j = index + 1; j < array.length; j++) {
            if (array[j] < currentMin) {
                currentMin = array[j];
                indexOfMin = j;
                swapNeeded = true;
            }
        }
        if (swapNeeded) {
            swap(array, index, indexOfMin);
            return;
        }
    }
}

private static void swap(int[] a, int i, int j) {
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}

selectionSort() has to be called 7 times to sort this array.

But if you consider array like {3, 5, 1, 4, 7, 6, 2}, 3rd and 4rd indices of the array are already in the sorted positions. Therefore, selectionSort() has to be called only 5 times.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.