# Thread: Selection Sort. please help!

1. Member
Join Date
Mar 2011
Posts
2
Rep Power
0

## Selection Sort. please help!

I am trying to sort some fractions that are read in from a file. They are read in fine but i am having trouble with the sorting method. I have a fractions class that was given to us so i know i do not need to change anything in it:
Java Code:
```import java.util.*;

public class Fraction implements Comparator<Fraction>
{
//the numerator of the fraction
private int numerator;

//the denominator of the fraction
private int denominator;

//---------------------------------------

//  Constructors

//-----------------------------------------

//creates a fraction 0/1

public Fraction()
{
this(0,1);
}

//Creates a fraction  number/1

public Fraction(int number)
{
this(number,1);
}

//Creates a fraction numerator/denominator passed as parameters

public Fraction(int numerator, int denominator)
{
//One can not have a zero denominator
if (denominator == 0)
{
System.out.println("Fatal Error: Can not set denominator to zero");
System.exit(1);
}

//Make the numerator "store" the sign
if (denominator < 0)
{
numerator = numerator * (-1);
denominator *= -1;
}

this.numerator = numerator;
this.denominator = denominator;

reduce();
}

//-----------------------------------------------------------
// Returns the sum of the passed Fraction to the Fraction
//------------------------------------------------------------

public Fraction add(Fraction frac)
{
int a,b,c,d;

a = this.numerator;
b = this.denominator;
c = frac.getNumerator();
d = frac.getDenominator();

return new Fraction(a*d + b*c,b*d);

}

//-----------------------------------------------------------
// Returns the difference of the passed Fraction to the Fraction
//------------------------------------------------------------

public Fraction subtract(Fraction frac)
{
int a,b,c,d;

a = numerator;
b = denominator;
c = frac.getNumerator();
d = frac.getDenominator();

return new Fraction(a*d - b*c,b*d);

}

//-----------------------------------------------------------
// Returns the product of the passed Fraction to the Fraction
//------------------------------------------------------------

public Fraction multiply(Fraction frac)
{
int a,b,c,d;

a = numerator;
b = denominator;
c = frac.getNumerator();
d = frac.getDenominator();

return new Fraction(a*c,b*d);

}

//-----------------------------------------------------------
// Returns the division of the passed Fraction to the Fraction
//------------------------------------------------------------
public Fraction divide(Fraction frac)
{
return multiply(frac.reciprocal());
}

//------------------------------------------------------
//Returns the reciprocal of this fraction
//----------------------------------------------------
public Fraction reciprocal()
{
return new Fraction(denominator,numerator);
}

//-----------------------------------------------
//compares this fraction with the passed Fraction for equality.
//If they are equal, it returns true, else false
//-------------------------------------------------------
public boolean equals(Fraction frac)
{
return (numerator == frac.getNumerator()
&& denominator == frac.getDenominator());
}
//------------------------------------------------
//Does the assignment of values without changing addresses
//-------------------------------------------------------
public void assigns(Fraction frac)
{
numerator = frac.getNumerator();
denominator = frac.getDenominator();
}

//------------------------------------------------------
//This adds 1 to the fraction, so that -3/2 becomes -1/2
//------------------------------------------------------
public void increment()
{
numerator += Math.abs(denominator);
}

//implements the compare method of Comparator: it returns a
//negative number if the first Fraction sent is smaller than the second
//fraction sent, zero if equal, and a positive number if first Fraction sent
//is larger than the second Fraction sent

public int compare(Fraction frac1,Fraction frac2)
{
int a = frac1.getNumerator();
int b = frac1.getDenominator();
int c = frac2.getNumerator();
int d = frac2.getDenominator();

return a*d - b*c;
}

public int getNumerator()
{
return numerator;
}

//------------------------------------------------
//returns the value of denominator
//-------------------------------------------------------

public int getDenominator()
{
return denominator;
}

//---------------------------------------------------------
//Returns the String representation of this Fraction
//--------------------------------------------------------
public String toString()
{
String result;

if (numerator == 0)
result = "0";
else if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;

return result;
}

//-------------------------------------------------------
//This reduces the fraction by dividing both the numerator and the
//denominator by their greatest common divisor.
//-----------------------------------------------------------

private void reduce()
{
if (numerator != 0)
{
int common = gcd(Math.abs(numerator),denominator);

numerator = numerator / common;
denominator = denominator / common;
}
else
denominator = 1;
}

//---------------------------------------------------------------
//This computes and returns the greatest common divisor of the two
//positive parameters.  It uses Eculid's algorithm
//----------------------------------------------------------------

private int gcd(int m, int n)
{
int r;

//Make sure that m is the smaller of the two numbers
if (m > n)
{
r = m;
m = n;
n = r;
}

r = n % m;

while(r != 0)
{
n = m;
m = r;
r = n % m;
}

return m;
}
}```
Here is the method for selection sort. i have tried everything i can possibly think of and nothing will work.
Java Code:
``` private void selectionSort()
{

int minIndex=0,i=0,smallestIndex=0,temp=0;
Fraction f = new Fraction();

for(i = 0; i < n - 1; i++)
{
smallestIndex = i;
for(minIndex = i+1; minIndex < n; minIndex++)
{
if(f.compare(A[minIndex],A[smallestIndex])<0)
{
A[minIndex] = A[temp];

//i = A[smallestIndex];
}

}
A[smallestIndex] = A[temp];
A[temp] = A[i];
A[i] = A[minIndex];

}

}```

2. Member
Join Date
Mar 2011
Posts
2
Rep Power
0
im supposed to write the sorting algorithm myself but to use this would i just do Arrays.sort(name of array)?

3. Automatically sorts in ascending order:

Java Code:
`java.util.Arrays.sort(yourArray);`
Sort array in reverse order:

Java Code:
`java.util.Arrays.sort(yourArray, Collections.reverseOrder());`

And are you sure you have to Manually sort your own array out? I don't see whats wrong in using a built-in function.
Last edited by ozzyman; 03-14-2011 at 09:38 PM.

4. Grab a piece of paper and a pen. Pretend you are the computer executing the code. Write down the variables and their values and each time they change in the code, update your paper. This will help you track down where your algorithm is going wrong.
Java Code:
```A[smallestIndex] = A[temp];
A[temp] = A[i];```
One thing I noticed is that you set temp to 0 and never change it. So you will always be swapping values at the first position in the array.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•