Sorting numbers as they are entered?
Author |
Message |
Krocker
|
Posted: Sun Jan 12, 2014 3:59 pm Post subject: Sorting numbers as they are entered? |
|
|
hi, so i have to create a program which creates and array with a user specified size and intially fill it with 0. Then ask the users to enter a number to fill each element. However, each time the uer inputs a number, the number must be put into the array in order. I kinda got it working. If i enter a number that is less then the numbers already entered, it works only if there is one other number in the array, else it replaces the other arrays that are greater then it.
ex. Array currently: 5 7 0 0 0
input: 4
Array Now: 4 5 0 0 0
code: | package question2;
import java.util.Scanner;
public class SortedArray {
public static void main(String[] args) {
Scanner keyb = new Scanner (System.in);
int size = 0;
//gets the user to input the size of the array
do{
System.out.print("Enter number of values in the array (1 to 10): ");
size = keyb.nextInt();
}while (size < 1 || size > 10);
//creates the array using the size inputted
int[] array = new int[size];
//fills the array with 0 initially and outputs the array
System.out.println ("\nHere's your starting array:");
for (int i = 0; i < size; i++){
array[i] = 0;
System.out.print(array[i] + " ");
}
int numElement = 1;//keeps track of the number of elements entered
int val = 0;
//prompts user to enter a value to replace the 0
System.out.print ("\nEnter an integer value: ");
val = keyb.nextInt();
array[0] = val;
//displays the updated array
System.out.print ("\nUpdated array: ");
for (int i = 0; i < size; i++){
System.out.print(array[i] + " ");
}
//prompts user to enter a value to replace the 0 again
while (numElement <= array.length){
System.out.print ("\nEnter an integer value: ");
val = keyb.nextInt();
//sorts the array with the new values
if (array[numElement-1] < val){
array[numElement] = val;
}else if (array[numElement-1] > val){
for (int i = 0; i < numElement; i++){
if (array[i] > val){
int temp = array[i];
array[i] = val;
array[i+1] = temp;
}
}
}
numElement++;
//displays the sorted array
System.out.print ("\nUpdated array: ");
for (int i = 0; i < size; i++){
System.out.print(array[i] + " ");
}
}
}
} |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Sun Jan 12, 2014 4:29 pm Post subject: RE:Sorting numbers as they are entered? |
|
|
When you insert a new entry, your code only shifts the entry that the new entry is replacing. You need an iterative or recursive solution to shift all following elements. For example, in pseudocode:
code: | function insert (element i, position p, array a){
if p < length (a) {
var temp = a(p)
a(p) = i
insert (temp, p+1, a)
}
else a(p) = i
} |
Alternatively, you can use a linked list, which allows very easy inserts mid-list. |
|
|
|
|
|
Krocker
|
Posted: Sun Jan 12, 2014 9:08 pm Post subject: Re: Sorting numbers as they are entered? |
|
|
ok.. what im not sure about is the position p, what is that?
So this is what i came u with according to what u said, not sure if its right though cuz its still not working:
code: | public static int[] sorting (int[] a , int val, int p){
if (p < a.length-1) {
int temp = a[p];
a[p] = val;
sorting (a,temp , p+1);
}else{
a[p] = val;
}
return a;
} |
and this is whats being fed into the method:
array = sorting(array,val,0)
Help anyone? im totally lost for this one. |
|
|
|
|
|
|
|