codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Using Selection sort for a linked list  alainap21 at 14:09 on Thursday, April 27, 2006
 

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;
}




  Re: Using Selection sort for a linked list  crwood at 09:49 on Saturday, April 29, 2006
 


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;
}
}
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: Help !! -- Array Declaration --
•  loading external code
•  submit button
•  SQL stored procedure error
•  Needs Java script help
•  Problem with XML
•  RMI server problem
•  Re: help please for newbie to java and i/o
•  Help !! -- Array Declaration --


Recent Articles
What is a pointer in C?
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net


© Copyright codetoad.com 2001-2006