Hello,

I have this code that sorts strings but it does not sort them at all. I've been working on it for 2 hours already. Any help will be appreciated.

Thanks in advance :)

public void SelectionSort() {
        for (int i = 0; i < myList.length - 1; i++) {
            int smallist = getSmallest(i, myList.length-2);
            swap(i, smallist);
        }

    }

    private int getSmallest(int a, int b) {
        int small = a;
        for (int i = a+1; i < myList.length; i++) {
            if (myList[i].compareTo(myList[a]) > 0) {
                myList[a] = myList[i];
            }
        }
        return small;
    }

Hello,

I have this code that sorts strings but it does not sort them at all. I've been working on it for 2 hours already. Any help will be appreciated.

Thanks in advance :)

What exactly does the code do?

I have this selection sort from one of our laboratory assignments:

/**
	 * Sorts an array into non-decreasing order.  This method uses a selection
	 * sort algorithm, in which the largest item is found and placed at the end of 
	 * the list, then the second-largest in the next to last place, and so on.
	 */
	 public static void selectionSort(int[] array) {
		for (int top = array.length - 1; top > 0; top--) {
			int positionOfMax = 0;
			for (int i = 1; i <= top; i++) {
				if (array[i] > array[positionOfMax])
					positionOfMax = i;
			}
			int temp = array[top];  // swap top item with biggest item
			array[top] = array[positionOfMax];
			array[positionOfMax] = temp;
		}
	}

What exactly with this code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.