Hello everyone,

I'm working on a school project and am using some code from the book to sort an array in ascending order. I'm apologize ahead of time if my methods seem crude.

I've used this sort method on a normal array of numbers and the numbers were ordered correctly; however, when I'm using it in tandem with another array it seems to sort incorrectly. Here is an example of what I'm saying:

*****************************************
STAFF
*****************************************
Anne		30
Bob 		150
Ralph		305
Tim 		225
Barbara		135
Jane		160
Steve		80
Tom 		200
Mike		165
Shirley		90
Pam 		100
Frank		120
*****************************************
PASSENGERS ON BOARD
*****************************************
Bob 		150 lbs
Pam 		100 lbs
Barbara		135 lbs
Jane		160 lbs
Anne		30 lbs
Frank		120 lbs
Tom 		200 lbs
Mike		165 lbs
CURRENT WEIGHT - 1060
OVERLOAD PASSENGER - Steve		80 lbs

SORTED:
Steve		80
Frank		120
Anne		30
Barbara		135
Pam 		100
Bob 		150
Jane		160
Mike		165
Tom 		200

It appears the sort is working in small segments! I am not sure why this is...here is the sort itself:

int sortCounter = 0;
	int minIndex;
	for (int i=0; i < sortableWeights.length; i++)
	    {
			//find smallest element in list starting at location i
			minIndex = i;
			for (int j = i+1; j < sortableWeights.length; j++)
			 {  
				 if (sortableWeights[j] < sortableWeights[minIndex])
				  {  
					 minIndex = j;
					//swap list[i] with smallest element
					int temp = sortableWeights[i];
					String temp2 = sortableNames[i];
					sortableWeights[i] = sortableWeights[minIndex];
					sortableNames[i] = sortableNames[minIndex];
					sortableWeights[minIndex] = temp;
					sortableNames[minIndex] = temp2;
					sortCounter++;
				  }
		    }	
		}

Below is the same sorting code, as it was naturally given to me. This code works with all kinds of numbers.

public void selectionSort()
    {
	int minIndex;
	for (int i=0; i < list.length-1; i++)
	    {
		//find smallest element in list starting at location i
		minIndex = i;
		for (int j = i+1; j < list.length; j++)
		    if (list[j] < list[minIndex])
			    minIndex = j;

		//swap list[i] with smallest element
		int temp = list[i];
		list[i] = list[minIndex];
		list[minIndex] = temp;
	    }
    }

The code below shows how the arrays are being initialized. They are designed to use a random number to determine what data from the hard coded arrays should fit into the incrementing index of the sortable arrays. If the data the random number selects is equal to data already in an element of the sortable arrays, then the counter is not incremented and the loop repeats.

int [] sortableWeights = new int [20];
	String [] sortableNames = new String [20];

		int randomSelect = Math.abs(generator.nextInt()) % 12;
		String temp = names[randomSelect]+"\t\t"+Integer.toString(weights[randomSelect])+" lbs";
		
		currentLoad[counter] = temp;
		sortableWeights[counter] = weights[randomSelect];
		sortableNames[counter] = names[randomSelect];

Let me know if you guys have any thoughts, or if there is any more information I should add!

Hello everyone,

I'm working on a school project and am using some code from the book to sort an array in ascending order. I'm apologize ahead of time if my methods seem crude.

I've used this sort method on a normal array of numbers and the numbers were ordered correctly; however, when I'm using it in tandem with another array it seems to sort incorrectly. Here is an example of what I'm saying:

*****************************************
STAFF
*****************************************
Anne		30
Bob 		150
Ralph		305
Tim 		225
Barbara		135
Jane		160
Steve		80
Tom 		200
Mike		165
Shirley		90
Pam 		100
Frank		120
*****************************************
PASSENGERS ON BOARD
*****************************************
Bob 		150 lbs
Pam 		100 lbs
Barbara		135 lbs
Jane		160 lbs
Anne		30 lbs
Frank		120 lbs
Tom 		200 lbs
Mike		165 lbs
CURRENT WEIGHT - 1060
OVERLOAD PASSENGER - Steve		80 lbs

SORTED:
Steve		80
Frank		120
Anne		30
Barbara		135
Pam 		100
Bob 		150
Jane		160
Mike		165
Tom 		200

It appears the sort is working in small segments! I am not sure why this is...here is the sort itself:

int sortCounter = 0;
	int minIndex;
	for (int i=0; i < sortableWeights.length; i++)
	    {
			//find smallest element in list starting at location i
			minIndex = i;
			for (int j = i+1; j < sortableWeights.length; j++)
			 {  
				 if (sortableWeights[j] < sortableWeights[minIndex])
				  {  
					 minIndex = j;
					//swap list[i] with smallest element
					int temp = sortableWeights[i];
					String temp2 = sortableNames[i];
					sortableWeights[i] = sortableWeights[minIndex];
					sortableNames[i] = sortableNames[minIndex];
					sortableWeights[minIndex] = temp;
					sortableNames[minIndex] = temp2;
					sortCounter++;
				  }
		    }	
		}

Below is the same A code, as it was naturally given to me. This code works with all kinds of numbers.

public void selectionSort()
    {
	int minIndex;
	for (int i=0; i < list.length-1; i++)
	    {
		//find smallest element in list starting at location i
		minIndex = i;
		for (int j = i+1; j < list.length; j++)
		    if (list[j] < list[minIndex])
			    minIndex = j;

		//swap list[i] with smallest element
		int temp = list[i];
		list[i] = list[minIndex];
		list[minIndex] = temp;
	    }
    }

The code below shows how the arrays are being initialized. They are designed to use a random number to determine what data from the hard coded arrays should fit into the incrementing index of the sortable arrays. If the data the random number selects is equal to data already in an element of the sortable arrays, then the counter is not incremented and the loop repeats.

int [] sortableWeights = new int [20];
	String [] sortableNames = new String [20];

		int randomSelect = Math.abs(generator.nextInt()) % 12;
		String temp = names[randomSelect]+"\t\t"+Integer.toString(weights[randomSelect])+" lbs";
		
		currentLoad[counter] = temp;
		sortableWeights[counter] = weights[randomSelect];
		sortableNames[counter] = names[randomSelect];

Let me know if you guys have any thoughts, or if there is any more information I should add!

int [] sortableWeights = new int [20];
String [] sortableNames = new String [20];

int randomSelect = Math.abs(generator.nextInt()) % 12;
String temp = names[randomSelect]+"\t\t"+Integer.toString(weights[randomSelect])+" lbs";

currentLoad[counter] = temp;
sortableWeights[counter] = weights[randomSelect];
sortableNames[counter] = names[randomSelect]; int [] sortableWeights = new int [20];
String [] sortableNames = new String [20];

int randomSelect = Math.abs(generator.nextInt()) % 12;
String temp = names[randomSelect]+"\t\t"+Integer.toString(weights[randomSelect])+" lbs";

currentLoad[counter] = temp;
sortableWeights[counter] = weights[randomSelect];
sortableNames[counter] = names[randomSelect];
its correct and its working

This article has been dead for over six months. Start a new discussion instead.