|
im creating a linked list of nodes that each contain an integer. Then im going to use selection sort to sort my integers. Im a little confused on implementing selection sort into my linked list....this is what i have so far...any advice would be great....i know a lot of this code could be shortened up but im stuck at the moment.
public class Sorting
{
/** Here is my sorting class*/
public static void selectionSort(Comparable[] list)
{
int min;
Comparable temp;
for(int index = 0; index < list.length-1; index++)
{
min = index;
for(int scan = index+1; scan < list.length; scan++)
if(list[scan].compareTo(list[min]) < 0)
min = scan;
temp = list[min];
list[min] = list[index];
list[index] = temp;
}}}
public class IntegerSort
{
/** this is my main driver class*/
public static void main(String[] args)
{
IntegerList num = new IntegerList();
num.add(new Integer("1"));
num.add(new Integer("2"));
num.add(new Integer("3"));
num.add(new Integer("4"));
num.add(new Integer("5"));
num.add(new Integer("6"));
num.add(new Integer("7"));
num.add(new Integer("8"));
Sorting.selectionSort(num); //i get an error here saying cannot find symbol method selectionSort
for(Integer i : num) //and an error here saying for each not applicable for expression type
System.out.println(i);
}}
public class IntegerList
{
private IntegerNode list;
/** this will represent my collection of integers*/
public IntegerList()
{
list = null;
}
public void add(Integer num)
{
IntegerNode node = new IntegerNode (num);
IntegerNode current;
if(list==null)
list = node;
else
{
current = list;
while(current.next != null)
current = current.next;
current.next = node;
}
}
public String toString()
{
String result = "";
IntegerNode current = list;
while(current !=null)
{
result +=current.integers + "\n";
current = current.next;
}
return result;
}
private class IntegerNode
{
public Integer integers;
public IntegerNode next;
public IntegerNode (Integer num)
{
integers = num;
next = null;
}
}}
public class Integer
{
private int number;
/** this is my integer class*/
public Integer(int Newnumber)
{
number = Newnumber;
}
public int getNum()
{
return number;
}
|
|
|
public class IntNumberSort
{
/** this is my main driver class*/
public static void main(String[] args)
{
IntegerList num = new IntegerList();
java.util.Random r = new java.util.Random();
for(int j = 0; j < 10; j++)
num.add(new Integer(r.nextInt(101)));
System.out.println(num);
IntegerList.IntegerNode[] array = num.toArray();
Sorting.selectionSort(array);
System.out.println("array = " + java.util.Arrays.toString(array));
}
}
class IntegerList
{
private IntegerNode list;
/** this will represent my collection of integers*/
public IntegerList()
{
list = null;
}
public void add(Integer num)
{
IntegerNode node = new IntegerNode (num);
IntegerNode current;
if(list==null)
list = node;
else
{
current = list;
while(current.next != null)
current = current.next;
current.next = node;
}
}
protected IntegerNode[] toArray()
{
int count = 0;
IntegerNode current = list;
while(current != null)
{
count++;
current = current.next;
}
IntegerNode[] retArray = new IntegerNode[count];
current = list;
count = 0;
while(current != null)
{
retArray[count++] = current;
current = current.next;
}
//System.out.println(java.util.Arrays.toString(retArray));
return retArray;
}
public String toString()
{
String result = "IntegerList[";
IntegerNode current = list;
while(current != null)
{
result += current.integers;
current = current.next;
if(current != null)
result += ", ";
}
return result + "]";
}
public class IntegerNode implements Comparable
{
public Integer integers;
public IntegerNode next;
public IntegerNode (Integer num)
{
integers = num;
next = null;
}
public int compareTo(Object o)
{
IntegerNode node = (IntegerNode)o;
return integers < node.integers ? -1 :
integers > node.integers ? 1 : 0;
}
public String toString()
{
return integers.toString();
}
}
}
class Sorting
{
/** Here is my sorting class*/
public static void selectionSort(Comparable[] list)
{
int min;
Comparable temp;
for(int index = 0; index < list.length-1; index++)
{
min = index;
for(int scan = index+1; scan < list.length; scan++)
if(list[scan].compareTo(list[min]) < 0)
min = scan;
temp = list[min];
list[min] = list[index];
list[index] = temp;
}
}
}
|
|
|
|
|
|
|
|
|
|