Can someone please help me to sort the values using the queue method. I am stuck don't know how to proceed with my assignment due tomorrow morning. Please answer to this. Thankyou in advance.
public class Lab2Queue
{
public static void main (String args[])
{
int [] initialArray, sortedArray;
initialArray = new int [100];
sortedArray = new int [100];
for (int i = 0; i < initialArray.length; i++)
initialArray[i] = i;
for (int i = 0; i < initialArray.length; i++)
{
int pos = (int) Math.floor(Math.random()*initialArray.length);
int temp = initialArray[i];
initialArray[i] = initialArray[pos];
initialArray[pos] = temp;
}
// display to randomized values
displayValues (initialArray);
// sort the values using the queue method as described
// in the lab write up. The sorted array should then be placed
// into the variable sortedArray
Queue q1 = new Queue(100);
// insert your code here
// display the sorted values
displayValues (sortedArray);
}
public static void displayValues (int arr[])
{
for (int i = 0; i < arr.length; i++)
{
if (i == 0)
System.out.print ("Pos 0: ");
else if (i % 10 == 0)
System.out.print ("Pos " + i + ": ");
String num;
if (arr[i] < 10)
num = " " + arr[i];
else
num = "" + arr[i];
System.out.print (num);
if (i % 10 == 9)
System.out.println("");
else
System.out.print (", ");
}
System.out.println("");
}
}
class Queue
{
private int maxSize;
private long[] initialArray;
private int front;
private int rear;
private int size;
public Queue (int s)
{
maxSize = s;
initialArray = new long[maxSize];
front = 0;
rear = -1;
size = 0;
// insert your code here
}
public boolean isEmpty()
{
// insert your code here
return (size == 0);
}
public void enqueue (int s)
{
// insert your code here
if (size == initialArray.length)
throw new QueueOverFlowException();
else
{
//Add to rear
size ++;
initialArray[rear] = s;
rear ++;
if (rear == initialArray.length) rear = 0;
}
}
public int dequeue()
{
// insert your code here
if (isEmpty())
throw new EmptyQueueException();
else
{
size --;
//remove from front
String value = initialArray[front];
//facilitate garbage collection
initialArray[front] = null;
//update front
front++;
if (front == initialArray.length) front = 0;
return value;
}
}
public int front ()
{
return initialArray[front];
// insert your code here
}
}
public class Lab2Queue
{
public static void main (String args[])
{
int [] initialArray, sortedArray;
initialArray = new int [100];
sortedArray = new int [100];
for (int i = 0; i < initialArray.length; i++)
initialArray[i] = i;
for (int i = 0; i < initialArray.length; i++)
{
int pos = (int) Math.floor(Math.random()*initialArray.length);
int temp = initialArray[i];
initialArray[i] = initialArray[pos];
initialArray[pos] = temp;
}
// display to randomized values
displayValues (initialArray);
// sort the values using the queue method as described
// in the lab write up. The sorted array should then be placed
// into the variable sortedArray
Queue q1 = new Queue(100);
// insert your code here
// display the sorted values
displayValues (sortedArray);
}
public static void displayValues (int arr[])
{
for (int i = 0; i < arr.length; i++)
{
if (i == 0)
System.out.print ("Pos 0: ");
else if (i % 10 == 0)
System.out.print ("Pos " + i + ": ");
String num;
if (arr[i] < 10)
num = " " + arr[i];
else
num = "" + arr[i];
System.out.print (num);
if (i % 10 == 9)
System.out.println("");
else
System.out.print (", ");
}
System.out.println("");
}
}
class Queue
{
private int maxSize;
private long[] initialArray;
private int front;
private int rear;
private int size;
public Queue (int s)
{
maxSize = s;
initialArray = new long[maxSize];
front = 0;
rear = -1;
size = 0;
// insert your code here
}
public boolean isEmpty()
{
// insert your code here
return (size == 0);
}
public void enqueue (int s)
{
// insert your code here
if (size == initialArray.length)
throw new QueueOverFlowException();
else
{
//Add to rear
size ++;
initialArray[rear] = s;
rear ++;
if (rear == initialArray.length) rear = 0;
}
}
public int dequeue()
{
// insert your code here
if (isEmpty())
throw new EmptyQueueException();
else
{
size --;
//remove from front
String value = initialArray[front];
//facilitate garbage collection
initialArray[front] = null;
//update front
front++;
if (front == initialArray.length) front = 0;
return value;
}
}
public int front ()
{
return initialArray[front];
// insert your code here
}
}
0 Comments On This Entry
Trackbacks for this entry [ Trackback URL ]
← February 2022 →
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | ||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 |
Tags
My Blog Links
Recent Entries
-
Java queue sortingon Feb 07 2004 04:40 PM
Search My Blog
1 user(s) viewing
1 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)



Leave Comment








|