This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's
Calculator Programming subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.
General Coding and Design =>
Calculator Programming
Author |
Message |
|
bananaman Indestructible
Calc Guru
Joined: 12 Sep 2005 Posts: 1124
|
Posted: 08 Mar 2007 08:37:15 am Post subject: |
|
|
I am writing a program for a class and I seem to be stuck. The compiler keeps on giving me an error and I don't see the issue, maybe a fresh set of eyes can enlighten my on the reason.
Here is the code.
Code: import java.util.*;
/**
* Description of the Class
*
* @author Your Name Here
* @created Month Day, Year
*/
public class Sorts
{
long steps;
/**
* Description of Constructor
*
* @param list Description of Parameter
*/
Sorts()
{
steps = 0;
}
public void bubbleSort(int [ ] list)
{
for (int outer = 0; outer < list.length - 1; outer++)
{
for (int inner = 0; inner < list.length - outer-1; inner++)
{
if (list [inner] > list [inner + 1])
{
//swap list [inner] & list [inner+1]
int temp = list [inner];
list [inner] = list [inner + 1];
list [inner + 1] = temp;
}
this.steps++;
}
}
}
/**
* Description of the Method
*
* @param list reference to an array of integers to be sorted
*/
public void selectionSort(int[] list)
{
int min, temp;
for (int outer = 0; outer < list.length - 1; outer++)
{
min = outer;
for (int inner = outer + 1; inner < list.length; inner++)
{
if (list [inner] < list [min])
{
min = inner; // a new smallest item is found
}
this.steps++;
}
//swap list [outer] & list [flag]
temp = list [outer];
list [outer] = list [min];
list [min] = temp;
}
}
/**
* Description of the Method
*
* @param list reference to an array of integers to be sorted
*/
public void insertionSort(int[] list)
{
for (int outer = 1; outer < list.length; outer++)
{
int position = outer;
int key = list [position];
// Shift larger values to the right
while (position > 0 && list [position - 1] > key)
{
list [position] = list [position - 1];
position--;
this.steps++;
}
list [position] = key;
}
}
/**
* Takes in entire vector, but will merge the following sections
* together: Left sublist from a[first]..a[mid], right sublist from
* a[mid+1]..a[last]. Precondition: each sublist is already in
* ascending order
*
* @param a reference to an array of integers to be sorted
* @param first starting index of range of values to be sorted
* @param mid midpoint index of range of values to be sorted
* @param last last index of range of values to be sorted
*/
public void merge(int[] a, int first, int mid, int last)
{
int[] temp = new temp[a.length];
int tCounter = 0;
int fCounter = first;
int mCounter = mid;
while(fCounter < mid || mCounter <= last)
{
if(fCounter == mid)
{
temp[tCounter] = a[mCounter];
tCounter++;
mCounter++;
}
else if(mCounter > last)
{
temp[tCounter] = a[fCounter];
tCounter++;
fCounter++;
}
else if (a[fCounter] < a[mCounter])
{
temp[tCounter] = a[fCounter];
tCounter++;
fCounter++;
}
else if (a[mCounter] <= a[fCounter])
{
temp[tCounter] = a[mCounter];
tCounter++;
mCounter++;
}
}
}
/**
* Recursive mergesort of an array of integers
*
* @param a reference to an array of integers to be sorted
* @param first starting index of range of values to be sorted
* @param last ending index of range of values to be sorted
*/
public void mergeSort(int[] a, int first, int last)
{
if (last == first)
{
// do nothing
}
else if (last - first == 1)
{
if (a[last] > a[first])
{
int temp = a[last];
a[last] = a[first];
a[first] = a[temp];
}
}
else
{
mergeSort(a[], first, last - ((last - first) / 2));
mergeSort(a[], last - ((last - first) / 2), last);
merge(a[], first, last - ((last - first) / 2), last);
}
}
/**
* Accessor method to return the current value of steps
*
*/
public long getStepCount()
{
return steps;
}
/**
* Modifier method to set or reset the step count. Usually called
* prior to invocation of a sort method.
*
* @param stepCount value assigned to steps
*/
public void setStepCount(int stepCount)
{
steps = stepCount;
}
}
There is another class that I'm using to access this code, but I don't think it is neccessary to display it. The error I am gettin is at the line that says, " mergeSort(a[], first, last - ((last - first) / 2));"
It says ".class expected"
Any help would be appreciated. |
|
Back to top |
|
|
Arcane Wizard `semi-hippie`
Super Elite (Last Title)
Joined: 02 Jun 2003 Posts: 8993
|
Posted: 08 Mar 2007 09:04:45 am Post subject: |
|
|
a[] -> a |
|
Back to top |
|
|
todlangweilig
Advanced Member
Joined: 14 Feb 2006 Posts: 470
|
Posted: 08 Mar 2007 04:52:36 pm Post subject: |
|
|
As Arcane said, try getting rid of the brackets after 'a'. See below.
Code: else
{
mergeSort(a, first, last - ((last - first) / 2));
mergeSort(a, last - ((last - first) / 2), last);
merge(a, first, last - ((last - first) / 2), last);
}
|
|
Back to top |
|
|
bananaman Indestructible
Calc Guru
Joined: 12 Sep 2005 Posts: 1124
|
Posted: 08 Mar 2007 10:35:17 pm Post subject: |
|
|
yup, as I thought it would be something stupid like that for a syntax error. thank you for your fresh set of eyes. I will worry about making sure the logic is correct for my class and it's probably best that you don't give me help in that area if you see a problem. |
|
Back to top |
|
|
|
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.
»
Go to Registration page