i want to implement a selection sort method that takes an array of ints and sorts it in a descending order. however, the trick is to keep the original selection sort method unchanged but instead using simple arithmetic operations and without adding extra loops to swap the elements after the array finished sorting. this is my code and the idea is to store the position of the maximum value and the minimum value in local variables and swap them with the corresponding position after the inner loop finishes iteration. i even tried using a only one variable to find the lowest value and put it at the end of the array but i failed and i am getting the wrong results and i need help spotting the error. here is my code
public static void newSortMethod(int[]a){
for(int i = 0; i < a.length-1; i++){
int maxPosition=i;
int minPosition=i;
for(int j = i+1; j < a.length; j++){
if(a[j] < a[minPosition]){
minPosition = j;
}
if(a[j] > a[maxPosition]){
maxPosition = j;
}
}
swap(a,maxPosition,i);
swap(a,minPosition,a.length-i-1);
}
System.out.println();
}
public static void swap(int[]a, int i, int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void main(String[] args) {
int[] a = {2,6,3,9,5,4,8,7,0,13,-3,1};
newSortMethod(a);
}
here is the output of the program so far
-3 8 2 9 13 5 4 6 3 1 7 0
Your original algorithm is wrong. Firstly, the if blocks should compare to minPosition and maxPosition, not i. Secondly, if you are selecting both minimum and maximum, then your inner for loop should stop at a.length - i, not a.length (since the top i elements are also sorted). Doing both gives you this as the ascending order algorithm.
public static void newSortMethod(int[]a){
for(int i = 0; i < a.length; i++){
int maxPosition=i;
int minPosition=i;
for(int j = i+1; j < a.length - i; j++){
if(a[j] < a[minPosition]){
minPosition = j;
}
if(a[j] > a[maxPosition]){
maxPosition = j;
}
}
swap(a,maxPosition,i);
swap(a,minPosition,a.length-i-1);
}
}
To switch to descending order, simply add one line.
public static void newSortMethod(int[]a){
for(int i = 0; i < a.length; i++){
int maxPosition=i;
int minPosition=i;
for(int j = i+1; j < a.length - i; j++){
if(a[j] < a[minPosition]){
minPosition = j;
}
if(a[j] > a[maxPosition]){
maxPosition = j;
}
}
swap(a,minPosition,maxPosition); // <-- this line
swap(a,maxPosition,i);
swap(a,minPosition,a.length-i-1);
}
}
Errors
First off, let’s look for problems in your code. There’s a few, which happens a lot in programming.
Your code is still trying to sort ascending with swap(a,minPosition,i), and then trying to put the maximum value at the end, which isn’t what you want: you want to put maximum values at the beginning.
Your n is never modified, so you’ll keep printing 0.
Sample solution
Now let’s see something that works. I’m not totally sure what your ascending selection sort looked like, but I imagine it should be something like this:
public static void ascendingSortMethod(int[]a){
int n = 0; // this is only to count how many times the swap method was called
for(int i = 0; i < a.length-1; i++){
int minPosition = i;
for(int j = i+1; j < a.length; j++){
if(a[j] < a[minPosition]){
minPosition = j;
}
}
if(minPosition != i){ // check whether swap is necessary
swap(a,minPosition,i);
n ++;
}
}
System.out.println(n);
}
To make it sort in descending order, just switch the comparison operator (and possibly the minPosition identifier for clarity).
public static void newSortMethod(int[]a){
int n = 0; // this is only to count how many times the swap method was called
for(int i = 0; i < a.length-1; i++){
int maxPosition = i;
for(int j = i+1; j < a.length; j++){
if(a[j] > a[maxPosition]){ // switched comparison operator
maxPosition = j;
}
}
if(maxPosition != i){ // check whether swap is necessary
swap(a,maxPosition,i);
n ++;
}
}
System.out.println(n);
}
Related
Here is the code:
public static int MaxProduct(int... a){ // the max possible product from an array
int i = 0;
int j = 0;
int m = 0;
int n = a.length;
while (i<n){
j++;
while(j<n ){
if (a[i]*a[j] > m){
m = a[i]*a[j];
j++;
}
}
i++;
}
return m;
}
System.out.println(MaxProduct(1,2,3,4,5));
The algorithm seems to work as expected (after check and making a table of the debugger). For the first index of an array it checks all possible products and edits m accordingly from 1 through to 5. And then once j is equal to 5 a[j]understandably is out of bounds since there are only 5 elements in the array
I then see the arrayoutofbounds error in the debugger beside (again which is what id expect) but instead of i increasing, and the second while loop starting the cycle again, a[i] stays as 1, the algorithm concludes, and i get the output 5
How do i get this to output 20 (4x5)
You need to make two changes. Check below 2 TODOs.
public static int MaxProduct(int... a) { // the max possible product from an array
int i = 0;
int j = 0;
int m = 0;
int n = a.length;
while (i < n) {
j = i + 1; // TODO: 1
while (j < n) {
if (a[i] * a[j] > m) {
m = a[i] * a[j];
}
j++; // TODO:2
}
i++;
}
return m;
}
I need to make this insertion sort function essentially copy elements to the right until the value that needs to be moved would be in the correct position, however, with the code I'm using I typically end up getting garbage out, and have tried multiple iterations with the same result. I am at wits end as I see no reason why this shouldn't work.
public static void Sort(Comparable[] a) {
int n = a.length;
Comparable temp = 0;
int x;
// Starting with the element at index 1...
for (int i = 1; i < n; i++) {
// ...move to the left until we find one less
// than the current element.
for (int j = i; j > 0; j--) {
if (less(a[j], a[j - 1]))
{
temp = a[j];
for(x = j; x > 0 && less(temp, a[x]); x--)
{
a[x] = a[x - 1];
}
a[x] = temp;
//exch(a, j, j - 1);
}
else
break;
}
}
}
less(a, b) checks to see if a < b, by the way.
On the first iteration of the innermost loop, in this condition: x > 0 && less(temp, a[x]) you are checking whether the value you just stored in temp... is less than the value you just stored in temp, referred to by another name. This will always return false, causing the loop to never start. The end result is that the entire method is an expensive no-op. If you're testing it by sending in a randomly jumbled array, you'll end up with the array still randomly jumbled when it's done.
To fix this, simply subtract 1 from the index in that condition, making it x > 0 && less(temp, a[x - 1]).
The rest of your code looks correct, I think, though the loop with j is redundant and can be removed.
This should do the trick
public static void Sort(Comparable[] a) {
int n = a.length;
Comparable temp = 0;
int x;
// Starting with the element at index 1...
for (int i = 1; i < n; i++) {
// ...move to the left until we find one less
// than the current element.
for (int j = i; j > 0; j--) {
if (less(a[j], a[j - 1]))
{
temp = a[j];
for(x = j; x > 0 && less(temp, a[x-1]); x--)
{
a[x] = a[x - 1];
}
a[x] = temp;
//exch(a, j, j - 1);
}
else
break;
}
}
}
So my task is to read a file line by line and store the integers into an array. Then to add the integers in spots 1-5, 2-6, 3-7 etc. and store those into a new array.
In array 1 there is 4 more values than array 2. I need to compare these Arrays and see if array1 is 0.999 bigger than array2.
If it is indeed larger, I need to print out the LOCATION of the number in the array 1.
Right now my problem is my code is outputting that every number is larger than the corresponding number in array 2.
Code:
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class Asgn7
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner file = new Scanner(new File("asgn7data.txt"));
double[] array = new double[file.nextInt()];
double[] newArray = new double[array.length - 4];
double tempVal = 0;
int j = 0;
int count = 0;
while(file.hasNext())
{
for(int i = 0; i < array.length ; i++)
{
array[i] = file.nextInt();
}
for(j = 0; j < array.length - 4; j++)
{
for(int k = 0; k < 5; k++)
{
newArray[j] += array[j+k] / 5;
}
}
for(int i = 2; i < array.length; i++)
{
if(array[i] > (newArray[i-2] + 0.999));
{
count++;
tempVal = count;
}
System.out.println(tempVal);
}
}
}
}
The values which should be compared are from 3-13.
Judging by the picture, you are not placing the values in the correct index in the second array, or you are not matching the correct ones.
If you want it to look exactly like in the picture, the second array should be declared:
double[] newArray = new double[array.length - 2];
And the loop to fill it should be changed to:
for(j = 2; j < array.length - 2; j++)
{
for(int k = -2; k <= 2; k++)
{
newArray[j] += array[j+k] / 5;
}
}
This will put the averages in the third, fourth, fifth... elements in newArray. And now you can compare them directly:
for(int i = 2; i < array.length - 2; i++)
{
if(array[i] > (newArray[i] + 0.999))
{
count++;
tempVal = count;
}
System.out.println(tempVal);
}
If you want to save the two unused spaces, as you originally did, rather than responding exactly to the picture, then you should calculate the values as you originally did. But remember to compare each element to the one two places before it and stop 2 places before the end.
Instead of
for(int i = 2; i < array.length; i++)
use
for(int i = 2; i < array.length - 2; i++)
To print the location, your construct with the count and tempVal is unnecessary. You just need to print i+1. Also note that you have a ; after your if. This means it's an empty if, and the block after it is always performed. Never have a ; after an if, for, while etc.
Not clear with what you are asking for in your question but without questioning what's the logic, by just looking at your code:
for(int i = 2; i < array.length; i++)
{
if(array[i] > (newArray[i-2] + 0.999));
{
count++;
tempVal = count;
}
System.out.println(tempVal);
}
}
if you relocate the system.out line as follows, I think you will get what you expect as follows:
for(int i = 2; i < array.length - 2; i++)
{
if(array[i] > (newArray[i-2] + 0.999));
{
System.out.println(tempVal);
// count++;
// tempVal = count;
}
}
}
PS: Please note that I have also changed the boundary for the loop to stop iteration on 13th member of the array, instead of 15.
Are you sure you're parsing the numbers correctly?
See Java: Reading integers from a file into an array
Why don't you print them out after parsing for verification?
btw, this will overflow the index of the 2nd array (since it is created using new double[array.length - 4]):
for(int i = 2; i < array.length; i++)
so does your code run?
I have a question about bubble sort using an array. Here is some example code:
public class SortArray
{
public static void main(String[] args)
{
int[] arr = {4,6,4,2,764,23,23};
sort(arr);
}
static void sort(int[] arr)
{
int k;
for(int i = 0; i < arr.length; i++)
{
for(int j = i; j < arr.length-1; j++)
{
if(arr[i] < arr[j+1])
{
k = arr[j+1];
arr[j+1] = arr[i];
arr[i] = k;
}
}
System.out.print(arr[i] + " ");
}
}
}
There are two loops for checking the array, I understand the first loop is to go through each element in array, but how about the second one? Why is j starting from i and why is it going up to the length minus 1?
Additionally, can I use bubble sort to sort an ArrayList?
What you have there is called selection sort, not bubble sort.
Bubble sort only swaps adjacent elements, but you're going though the rest of the array and finding the minimum, which is exactly what selection sort does.
Since you use arr[j+1], you can rewrite the inner loop to use arr[j] and shift up the range by one, like this:
for(int j = i + 1; j < arr.length; j++)
{
if(arr[i] < arr[j])
{
k = arr[j];
arr[j] = arr[i];
arr[i] = k;
}
}
Now it's a bit more clear that you're looking at each other remaining element to find the minimum.
Yes, you can modify any sort that works on arrays to sort an ArrayList instead by just using ArrayList.get instead of array accesses and ArrayList.set instead of array assignments, i.e.:
for(int i = 0; i < arr.size(); i++)
{
for(int j = i + 1; j < arr.size(); j++)
{
if(arr.get(i) < arr.get(j))
{
int k = arr.get(j);
arr.set(j, arr.get(i));
arr.set(i, k);
}
}
}
Note that both selection sort and bubble sort are fairly inefficient (O(n^2)), there are more efficient sorting algorithms such as quick-sort or merge-sort (running in O(n log n)).
The first loop holds an element in i and the second one examines each element after i, namely j and compares it to the i it has holded. If they can be swapped, the algorithm swaps their positions and moves on. When the outer loop finishes an iteration, all elements prior to i are sorted, so j starts from i.
Do this once on paper and it all makes sense.
EDIT: As you have completed your algorithm, the reason for the second loops condition, is that you swap i with j+1. So if your j goes on to the last element of the array, the next element wouldn't exist and raises an exception. Also you can use this code to sort ArrayList. Just change the code swapping values with the appropriate setters and getters.
I'm looking for a bubblesort code in java that is opposite of the usual thing that I'm seeing when I search the internet.
I don't really understand the code below, all I know is that it sorts a bunch of numbers from lowest to highest. Is the code below modifiable so that instead of outputting the numbers from lowest to highest. It outputs it as highest to lowest?
int i;
int array[] = {12,9,4,99,120,1,3,10};
System.out.println("Values Before the sort:\n");
for(i = 0; i < array.length; i++)
System.out.print( array[i]+" ");
System.out.println();
bubble_srt(array, array.length);
System.out.print("Values after the sort:\n");
for(i = 0; i <array.length; i++)
System.out.print(array[i]+" ");
System.out.println();
System.out.println("PAUSE");
}
public static void bubble_srt( int a[], int n ){
int i, j,t=0;
for(i = 0; i < n; i++){
for(j = 1; j < (n-i); j++){
if(a[j-1] > a[j]){
t = a[j-1];
a[j-1]=a[j];
a[j]=t;
}
}
}
}
change
if(a[j-1] > a[j]){
to
if(a[j-1] < a[j]){
you could change the bubblesort to satisfy your needs or leave it as is and walk the sorted array backwards. for both, you should try to understand such a little piece of code instead of simply asking for the modified code.
Some words about your code:
If you move the swap-method out of your inner loop, it get's more readable, and more easy to reason about the independent parts.
public void swap (int i, int j, int [] arr) {
int tmp = arr [i];
arr [i] = arr [j];
arr [j] = tmp;
}
Sweet little methods are easy to understand and test, which is important.
Don't declare the index variables outside the for. This makes it harder to reason about your code - the variables are visible without necessity outside the loop. In the old code, you gain nothing from declaring tmp outside of the inner loop. Declaration is cost-free at runtime.
public static void bubbleSort (int a[], int n) {
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n-i); j++) {
if (a[j-1] > a[j]) {
swap (j, j-1, a);
}
}
}
}
// ... missing ...
Don't repeat yourself. Move duplicated code into a method.
public static void show (int [] arr)
{
for (int i : arr)
System.out.print (i + " ");
System.out.println ();
}
Sweet little methods are easy to test. Use the simplified for-loop, whenever possible, to avoid off-by-one-errors, and to be more robust to code changes - they work for Lists too, for example.
int array[] = {12, 9, 4, 99, 120, 1, 3, 10};
System.out.println ("Values Before the sort:\n");
show (array);
bubbleSort (array, array.length);
System.out.print ("Values after the sort:\n");
show (array);
System.out.println ("PAUSE");
}
With the simplified code, it get's more easy to reason about, what which part does.
if (a[j-1] > a[j]) {
needs just to be changed
if (a[j-1] < a[j]) {
to reverse the order.
for(i = array.length -1; i >=0; i--)
{
System.out.println(array[i]);
}
Should work. You start at the end of the array and go backwards