FAQ Database Discussion Community


What mistake am I doing in this code for selection sort?

sorting,selection-sort
var selectionSort = function(array) { var minIndex; for(var i = 0;i <array.length;i++){ minIndex = indexOfMinimum(array,i);} swap(array,minIndex,i); }; where indexOfMinimum is used to find the index of min value for the subarray starting at index i. And swap is a popularly known function....

Why is my program pausing after a selection sort function?

c++,sorting,selection-sort
I have a simple sorting program being compiled by Dev-C++ 4.9.8.0. I ran the program (yes this compiles) and it simply stops after displaying the line where the vector is displayed for the first time. Note - it does not freeze, it seems to just be taking a pause. In...

counting swap count and compares in selection sort

java,selection-sort
I'm kind of new to Java and I'm having trouble understanding a concept. In this code I need to add a swap count and compare count and this is what I got so far. public static void SelectionSort ( int [ ] num, int howmany ) { int i, j,...

My program throws an ArrayIndexOutOfBoundsException, but only sometimes

arrays,indexoutofboundsexception,selection-sort
I'm taking a computer science class at my high school, and we learned a lesson on selection sort today. I wrote a program (it's probably clumsy, but please bear with me, I'm learning) and it works in that it sorts, but sometimes it throws an ArrayIndexOutOfBoundsException. Only sometimes. I don't...

How to loop through Selection Sort in Java?

java,selection-sort
I want to show each iteration of the selection sort to print out how it works, how would I loop and print this? I have it printing out the output after its been sorting already. Here's my code: public class TestSelectionSort { public static void main(String[] args) { int list[]...

Why is my selection sort returning a value that is not in the original vector?

c++,sorting,selection-sort
I've been tinkering around with it for a while now and I'm so close! Now the output seems to be continuously printing a zero as the first value of the "sorted" vector. This is homework on how to create a selection sort in C++. Example Output Vector: 6, 2, 11,...

Selection sort didn't sort in descending order

java,arrays,sorting,selection-sort
Here is my homework: I want to know how many compares and exchanges take place in selection sort. When I declared my array in reverse or in descending order like this: int arr[] = { 5, 4, 3, 2, 1 }; …it's running fine and count compares and exchanges too...

Suggestions for my Selection Sort / Java

java,debugging,selection-sort
My Selection Sort algorithm is not working. I am getting the following errors: //Exception in thread "main" java.lang.NullPointerException Note: this is for a java class. I do not have a lot of experience. I am done with the assignment. I am trying to understand the reason why my sorting algorithm...

Merge Sort vs Selection Sort

java,algorithm,sorting,mergesort,selection-sort
I have written these 2 sorting algorithms and it appears that selection sort is faster than merge sort, surely this can't be right? My test data is 10 random arrays of size 5000 to 50000 where the largest possible numbers in the array is 100 Here is my selection sort...