Reputation: 1
My goal is to move the elements in the ar[] array into the sorted[] array and sort them from least to greatest. I'm having trouble with that part though because my loop is supposed to find the smallest element in the array and then replace the element with a large number. I think I have most of the code down but when I run the program, every element in the sorted[] array is 2. What am I doing wrong here?
public class Lab1
{
public static void main(String argv[])
{
int ar[] = { 7, 5, 2, 8, 4, 9, 6 };
int sorted[] = new int[ar.length];
int smallest = ar[0];
int smallestindex = 0;
for (int i=0; i<ar.length; i++)
{
for (int n=0; n<ar.length; n++)
{
if (ar[n] < smallest)
{
smallest = ar[n];
smallestindex = n;
}
}
sorted[i] = smallest;
ar[i] = 1000000;
}
// print sorted array:
for (int i=0; i<sorted.length; i++)
{
System.out.println("sorted[" + i + "] = " + sorted[i]);
}
}
}
Upvotes: 0
Views: 8487
Reputation: 136
It seems you want to sort by using an algorithm called 'Selection Sort', but your code is a poorly implement.
You can learn from this brother's question and check the answers below that: Selection Sort Example
Upvotes: 0
Reputation: 2880
Because each time you crush old value (big value) and you don't save it for shifting
smallest = ar[n]; smallestindex = n; You need to add another variable to change values between cells ,or use arraylist for easy way.
int ar[] = {7, 5, 2, 8, 4, 9, 6};
int sorted[] = new int[ar.length];
int smallest = ar[0];
for (int i = 0; i < ar.length ; i++) {
for (int j = i + 1; j < ar.length; j++) {
if (ar[i] > ar[j]) {
smallest = ar[j]; //save small value
ar[j] = ar[i];//transaction between two comparables cells
ar[i] = smallest;//set small value as first
}
sorted[i]=ar[i]; //set first small value
}
}
// print sorted array:
for (int i = 0; i < sorted.length; i++) {
System.out.println("sorted[" + i + "] = " + sorted[i]);
}
Upvotes: 0
Reputation: 43
I just can't understand, why do we require this, like sorting into another array. You can sort within the array using Arrays.sort() method, or if you want to sort in a different array, you can take following steps:
Copy the array to new array.
Sort the new array and then sort.
If you still want to go with your implementation, then I have refactored your code. Now your code works fine and looks like:
public class Prog1 {
public static void main(String[] args) {
int ar[] = { 7, 5, 2, 8, 4, 9, 6 };
int sorted[] = new int[ar.length];
int smallest = ar[0];
int smallestindex = 0;
for (int i = 0; i < ar.length; i++) {
for (int n = 0; n < ar.length; n++) {
if (ar[n] < smallest) {
smallest = ar[n];
smallestindex = n;
}
}
sorted[i] = smallest;
//Your mistake was here.
ar[smallestindex] = 1000000;
smallest=ar[0];
smallestindex=0;
}
// print sorted array:
for (int i = 0; i < sorted.length; i++) {
System.out.println("sorted[" + i + "] = " + sorted[i]);
}
}
}
Your mistake was that you were not reassigning 'smallest' variable. As it was pointing to the smallest element of array so it doesn't get updated in next run because no element in array is less than this variable. Hope you get it. If any questions, you still have, please ask.
Upvotes: 0
Reputation: 4869
What about something like this?
Short and sweet:
import java.util.Arrays;
public class Lab1
{
public static void main(String argv[])
{
int ar[] = { 7, 5, 2, 8, 4, 9, 6 };
int sorted[] = ar.clone();
Arrays.sort(sorted);
System.out.println("Original array: " + Arrays.toString(ar));
System.out.println("Sorted array: " + Arrays.toString(sorted));
}
}
Output:
Original array: [7, 5, 2, 8, 4, 9, 6]
Sorted array: [2, 4, 5, 6, 7, 8, 9]
Upvotes: 3
Reputation: 1008
oops, there is a problem inside your loop, which copy the smallest in ar[] into sorted[], which is 2, so you are getting 2 in every element inside sorted[].
for (int i=0; i<ar.length; i++)
{
for (int n=0; n<ar.length; n++)
{
if (ar[n] < smallest)
{
smallest = ar[n]; <----YOU ALWAYS GET 2 HERE
smallestindex = n;
}
}
sorted[i] = smallest; <---- AND SET 2 TO YOUR sorted[] HERE
ar[i] = 1000000;
}
I don't know if you want a solution or solve it yourself since your question is asking what's wrong, but here it is
for (int i=0; i<ar.length; i++)
{
for (int n=0; n<ar.length; n++)
{
if (ar[n] < smallest)
{
smallest = ar[n];
smallestindex = n;
ar[n] = 1000000; <----CHANGE THE SMALLEST ITEM HERE RATHER THAN OUTSIDE
}
}
sorted[i] = smallest;
}
btw, to make a sorted copy in a easier way:
Firsy make a copy:
int[] sorted = ar.clone();
Then, sort it using Java Util:
Arrays.sort(sorted);
Just 2 lines you get what you want.
Upvotes: 0
Reputation: 125
You didn't do any action when this condition is not true.
if (ar[n] < smallest)
So just smallest element can pass this check and goes on new array.
Also you can sort your array in better shape like this
List<Integer> ar = Arrays.as list ( *your numbers here*);
Collections.sort(ar):
Upvotes: 0
Reputation: 33
I don't have my pc now but I think the problem is that once you set the variable smallest with 2 the first time, it remains 2 forever and there is no other number smaller. So I think you are executing the internal if just once and the value is always 2.
Edit. I think that moving the smallest inizialization below the fist for should work.
Upvotes: 0