Jump to content

Sorting Values from Arrays

- - - - -

  • Please log in to reply
13 replies to this topic

#1
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
I had a thread about this and said that the problem was solved but it actually wasn't I was in a hurry and I only tested the program once and it was a coincidence that the numbers I entered came out in perfect order somehow so I thought the program worked and posted that the it was solved when it really wasn't. I'm making a new thread here just so there's no misunderstanding.
Here is my code and I think my swapping method isn't working which I need for the sorting algorithm to work which Lethalwire helped me figure out.
I also get an error for array out of bounds
import java.util.Scanner;

public class ArrayTestFA {

    public static void main(String[] args) {

    	

    	Scanner in = new Scanner (System.in);

    	int input;

    	

    	int var = 0;

    	System.out.println("How many values would you like your array to hold?");

    	var = in.nextInt();

       	int array[] = new int[var];

    	

    	

    	

    	

    	for(int count=0; count<array.length; count++){

    		System.out.println("Enter the value for array index: " + count);

    		input = in.nextInt();

     		

    	}

    	

    	int index = 0;

    	int smallestValueIndex = 0;

    	

    	for (index=0; index < array.length; index++){

		    smallestValueIndex = index;

    		for(int j = index + 1; j < array.length; j++){

    			if (array[j] < array[smallestValueIndex]){

    				smallestValueIndex = j;

    				

    			}

    			

    		}

    	    swap(array, array[index], array[smallestValueIndex] );

    	}

    	

    	printArray(array);

    	

    	

    }


	private static void swap(int[] array,int index, int smallestValueIndex) {

		

		int swap1 = array[index];

		array[index] = array[smallestValueIndex];

		array[smallestValueIndex] = swap1;

	}

	

	private static void printArray(int[] array){

		for(int count=0; count< array.length; count++){

			System.out.println(array[count]);

		}

	}


}



Edited by An Alien, 27 February 2011 - 09:08 PM.


#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

swap(array, array[index], array[smallestValueIndex] );

This is your problem.
it might make more sense if your swap method was named:

swap( int[] array, int pos1, int pos2 )

Meaning, you'll swap position 1 with position 2 from the array.
You're passing 2 VALUES from the array instead of 2 POSITIONS from the array

I think the code you're wanting is:

swap( array, index, smallestValueIndex );



#3
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
Okay, but I thought that I was correctly swapping the values.

I first make swap1 assigned to the value of array[index]
and then I make the value of array[index] to array[smallestValueIndex]
and finally I make the value of array[smallestValueIndex] equal swap1.

Isn't that what I'm supposed to be doing?

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
your swap method is correct.


    	for (index=0; index < array.length; index++){

		    smallestValueIndex = index;

    		for(int j = index + 1; j < array.length; j++){

    			if (array[j] < array[smallestValueIndex]){

    				smallestValueIndex = j;

    				

    			}

    			

    		}

    	    // this is wrong     swap(array, array[index], array[smallestValueIndex] );

            swap( array, index, smallestValueIndex );

    	}




#5
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
That just gives me 0 only for output.
import java.util.Scanner;

public class ArrayTestFA {

    public static void main(String[] args) {

    	

    	Scanner in = new Scanner (System.in);

    	int input;

    	

    	int var = 0;

    	System.out.println("How many values would you like your array to hold?");

    	var = in.nextInt();

       	int array[] = new int[var];

    	

    	

    	

    	

    	for(int count=0; count<array.length; count++){

    		System.out.println("Enter the value for array index: " + count);

    		input = in.nextInt();

     		

    	}

    	

    	int index = 0;

    	int smallestValueIndex = 0;

    	

    	for (index=0; index < array.length; index++){

		    smallestValueIndex = index;

    		for(int j = index + 1; j < array.length; j++){

    			if (array[j] < array[smallestValueIndex]){

    				smallestValueIndex = j;

    				

    			}

    			

    		}

            swap( array, index, smallestValueIndex );

            }

    	

    	printArray(array);

    	

    	

    }


	private static void swap( int[] array, int index, int smallestValueIndex ) {

		

		int swap1 = array[index];

		array[index] = array[smallestValueIndex];

		array[smallestValueIndex] = swap1;

	}

	

	private static void printArray(int[] array){

		for(int count=0; count< array.length; count++){

			System.out.println(array[count]);

		}

	}


}



#6
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I meant to say your swap method is incorrect****
I posted that too late at night.

This seems to work for me for all numbers

    	for (index=0; index < array.length; index++){

		    smallestValueIndex = index;

    		for(int j = index + 1; j < array.length; j++){

    			if (array[j] < array[smallestValueIndex]){

    				smallestValueIndex = j;

    				

    			}

    			

    		}

    	    swap(array, index, smallestValueIndex );

    	}

    	

    	printArray(array);

    	

    	

    }


#7
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
IDK, I copied and pasted exactly what you posted and I still get all 0s. Maybe it's some other part of my code.

Full code:
import java.util.Scanner;

public class ArrayTestFA {

    public static void main(String[] args) {

    	

    	Scanner in = new Scanner (System.in);

    	int input;

    	

    	int var = 0;

    	System.out.println("How many values would you like your array to hold?");

    	var = in.nextInt();

       	int array[] = new int[var];

    	

    	

    	

    	

    	for(int count=0; count<array.length; count++){

    		System.out.println("Enter the value for array index: " + count);

    		input = in.nextInt();

     		

    	}

    	

    	int index = 0;

    	int smallestValueIndex = 0;

    	

    	for (index=0; index < array.length; index++){

		    smallestValueIndex = index;

    		for(int j = index + 1; j < array.length; j++){

    			if (array[j] < array[smallestValueIndex]){

    				smallestValueIndex = j;

    				

    			}

    			

    		}

    	    swap(array, index, smallestValueIndex );

    	}

    	

    	printArray(array);

    	

    	

    }


	private static void swap( int[] array, int index, int smallestValueIndex ) {

		

		int swap1 = array[index];

		array[index] = array[smallestValueIndex];

		array[smallestValueIndex] = swap1;

	}

	

	private static void printArray(int[] array){

		for(int count=0; count< array.length; count++){

			System.out.println(array[count]);

		}

	}


}



#8
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
Can someone test out the code above and tell me what they get as the output?

#9
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Uhh... you never actually add input as a value into array:
    	for(int count=0; count<array.length; count++){

    		System.out.println("Enter the value for array index: " + count);

    		input = in.nextInt();

     		array[count] = input;

    	}
That should make it so it's no longer all 0's.
Wow I changed my sig!

#10
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
Thank you very much ZekeDragon and Letalwire! Hopefully I can turn this program in late and get a few points, lol.

#11
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Sorry late post, been caught up.

I see zeke already answered this question.
You were never storing your data into the array, which is odd because in a previous version of your code, you WERE storing your data.

#12
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
Yeah, IDK why eclipse has been active weird.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users