Teacher being weird?
Author |
Message |
Panphobia
|
Posted: Thu Dec 13, 2012 8:52 pm Post subject: Teacher being weird? |
|
|
So we did our sorting/searching assignment, my algorithms work perfectly fine, we had to do selection sort, bubble sort, quick sort, and insertion sort, ascending arrray, and descending, for some reason I did not get perfect even though I met all requirements and my code is right inline to his pseudocode, can you guys tell me what is wrong with my code please, I am really annoyed, it was also supposed to be in a GUI, so that is why I can only give methods code: | static int[] quik, insertion, selection, bubble;
//sorting times and loop counts
static long timeQ = 0, timeI = 0, timeS = 0, timeB = 0;
static int loopQ = 0, loopI = 0, loopS = 0, loopB = 0;
@Action
public void sort() {
//initializing the loop counters
loopQ = 0;
loopS = 0;
loopI = 0;
loopB = 0;
//the radio button that you press makes you initialize the arrays to
//the value selected
if (radio10.isSelected()) {
quik = new int[10];
insertion = new int[10];
selection = new int[10];
bubble = new int[10];
} else if (radio100.isSelected()) {
quik = new int[100];
insertion = new int[100];
selection = new int[100];
bubble = new int[100];
} else if (radio1000.isSelected()) {
quik = new int[1000];
insertion = new int[1000];
selection = new int[1000];
bubble = new int[1000];
} else {
quik = new int[5000];
insertion = new int[5000];
selection = new int[5000];
bubble = new int[5000];
}
//calls the method that fills arrays with the random numbers
randomNum();
//calls method that prints the array unsorted
printArrayOrig();
//calls sort methods according to if it is
//ascending or descending
if (radioAsc.isSelected()) {
insertionAsc();
long currentTime = System.currentTimeMillis();
quikAsc(0, quik.length - 1);
long finishTime = System.currentTimeMillis();
timeQ = finishTime - currentTime;
selectionAsc();
bubbleAsc();
} else {
insertionDes();
long currentTime = System.currentTimeMillis();
quikDes(0, quik.length - 1);
long finishTime = System.currentTimeMillis();
timeQ = finishTime - currentTime;
selectionDes();
bubbleDes();
}
//prints sorted array
printArraySorted();
//prints time and loops
printTime();
}
public void printTime() {
//making a new string with the times and loops
String phrase = "Selection Sort:\nNumber of times a loop was executed: " + loopS
+ "\nNumber of Milliseconds to complete sort: " + timeS
+ "\nBubble Sort:\nNumber of times a loop was executed: " + loopB
+ "\nNumber of Milliseconds to complete sort: " + timeB
+ "\nInsertion Sort:\nNumber of times a loop was executed: " + loopI
+ "\nNumber of Milliseconds to complete sort: " + timeI
+ "\nQuik Sort:\nNumber of times a loop was executed: " + loopQ
+ "\nNumber of Milliseconds to complete sort: " + timeQ;
//printing phrase to the text area
txtArea.setText(phrase);
}
//prints a sorted array
public void printArraySorted() {
String phrase = "";
for (int i = 0; i < selection.length; ++i) {
phrase += i + " : " + selection[i] + "\n";
}
txtSort.setText(phrase);
}
//prints the original unsorted array
public void printArrayOrig() {
String phrase = "";
for (int i = 0; i < selection.length; ++i) {
phrase += i + " : " + selection[i] + "\n";
}
txtOrig.setText(phrase);
}
//quik sort ascending
//divides a large list into two smaller sub-lists
//the low elements and the high elements, then recursively sort the sub-lists
public void quikAsc(int low, int high) {
if (low >= high) {
return;
}
int i = low;
int j = high;
int pivot = quik[(low + high) / 2];
while (i < j) {
while (quik[i] > pivot) {
loopQ++;
i++;
}
while (pivot > quik[j]) {
loopQ++;
j--;
}
if (i <= j) {
int temp = quik[i];
quik[i] = quik[j];
quik[j] = temp;
i++;
j--;
}
}
quikAsc(low, j);
quikAsc(i, high);
}
//quik sort descending
//divides a large list into two smaller sub-lists
//the low elements and the high elements, then recursively sort the sub-lists
public void quikDes(int low, int high) {
if (low >= high) {
return;
}
int i = low;
int j = high;
int pivot = quik[(low + high) / 2];
while (i < j) {
while (quik[i] < pivot) {
loopQ++;
i++;
}
while (pivot < quik[j]) {
loopQ++;
j--;
}
if (i <= j) {
int temp = quik[i];
quik[i] = quik[j];
quik[j] = temp;
i++;
j--;
}
}
quikDes(low, j);
quikDes(i, high);
}
//insertion sort descending
//Every repetition of insertion sort removes an element
//from the input data, inserting it into the correct position in the already-sorted list
public void insertionDes() {
long currentTime = System.currentTimeMillis();
for (int i = 1; i < 100; i++) {
int j = i;
int a = insertion[i];
while ((j > 0) && (insertion[j - 1] < a)) {
loopI++;
insertion[j] = insertion[j - 1];
j--;
}
insertion[j] = a;
}
long finishTime = System.currentTimeMillis();
timeI = finishTime - currentTime;
}
//insertion sort ascending
//Every repetition of insertion sort removes an element
//from the input data, inserting it into the correct position in the already-sorted list
public void insertionAsc() {
long currentTime = System.currentTimeMillis();
for (int i = 1; i < insertion.length; i++) {
int j = i;
int a = insertion[i];
while ((j > 0) && (insertion[j - 1] > a)) {
loopI++;
insertion[j] = insertion[j - 1];
j--;
}
insertion[j] = a;
}
long finishTime = System.currentTimeMillis();
timeI = finishTime - currentTime;
}
//selection sort ascending
//checks for the minimum then puts it at the beginning of the array
//then adding 1 to the index of where it put the smallest value
public void selectionAsc() {
long currentTime = System.currentTimeMillis();
for (int i = 0; i < selection.length - 1; i++) {
for (int j = i + 1; j < selection.length; j++) {
loopS++;
if (selection[i] > selection[j]) {
int temp = selection[i];
selection[i] = selection[j];
selection[j] = temp;
}
}
}
long finishTime = System.currentTimeMillis();
timeS = finishTime - currentTime;
}
//selection sort descending
//checks for the minimum then puts it at the end of the array
//then subtracting 1 to the index of where it put the smallest value
public void selectionDes() {
long currentTime = System.currentTimeMillis();
for (int i = 0; i < selection.length - 1; i++) {
for (int j = i + 1; j < selection.length; j++) {
loopS++;
if (selection[i] < selection[j]) {
int temp = selection[i];
selection[i] = selection[j];
selection[j] = temp;
}
}
}
long finishTime = System.currentTimeMillis();
timeS = finishTime - currentTime;
}
//bubble sort ascending
//bubbles biggest numbers to the bottom of the array
public void bubbleAsc() {
long currentTime = System.currentTimeMillis();
boolean doMore = true;
int button = bubble.length - 1;
while (doMore) {
doMore = false;
for (int i = 0; i < button; i++) {
loopB++;
if (bubble[i] > bubble[i + 1]) {
int temp = bubble[i];
bubble[i] = bubble[i + 1];
bubble[i + 1] = temp;
doMore = true;
}
}
button--;
}
long finishTime = System.currentTimeMillis();
timeB = finishTime - currentTime;
}
//bubble sort descending
//bubbles largest numbers to the top of the array
//compares if bigger, if not stays
public void bubbleDes() {
long currentTime = System.currentTimeMillis();
boolean doMore = true;
while (doMore) {
doMore = false;
for (int i = 0; i < bubble.length - 1; i++) {
loopB++;
if (bubble[i] > bubble[i + 1]) {
int temp = bubble[i];
bubble[i] = bubble[i + 1];
bubble[i + 1] = temp;
doMore = true;
}
}
}
long finishTime = System.currentTimeMillis();
timeS = finishTime - currentTime;
}
public void randomNum() {
int ranNum = 0;
for (int i = 0; i < quik.length; ++i) {
ranNum = (int) (Math.random() * 20000) - 10000;
quik[i] = ranNum;
insertion[i] = ranNum;
selection[i] = ranNum;
bubble[i] = ranNum;
}
} |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Zren
|
Posted: Thu Dec 13, 2012 10:32 pm Post subject: RE:Teacher being weird? |
|
|
Why did you use arrays declared outside of a function instead of passing the array as a parameter? Essentially making the function only work for one array.
Typos: //quik sort ascending
Why do you initialize the variables when you declare them, if you re-intialize them right away?
Further learning:
Note how the difference between ASC and DESC is usually only the one comparison. Java has an interface for this specific area: Comparator. |
|
|
|
|
|
Panphobia
|
Posted: Thu Dec 13, 2012 10:42 pm Post subject: RE:Teacher being weird? |
|
|
Ok zren do you want me to screen shot the requirments? he wanted seperate methods fr ascending and descending and always docs marks for not initializing like that, WEIRD TEACHER |
|
|
|
|
|
crossley7
|
Posted: Fri Dec 14, 2012 1:51 am Post subject: RE:Teacher being weird? |
|
|
Coming on here is not where you are going to find your answer. None of us know how your teacher marks and therefore we can't tell you why he docked marks. your best chance is to just go to him and ask him yourself. Also watch your spelling. There are a bunch of errors (docks not docs).
Now in terms of conventions, your code seems long and the suggestions zren made are good to help with a bunch of the code specific problems. When designing something, you want to make your code as flexible as possible while maintaining efficiency since even if you think you will never need it again, you never know. (I recently went through a 1500+ line, 10 file program to modify a fixed array to be defined relative to a constant so it can be changed to my liking with ease) |
|
|
|
|
|
Panphobia
|
Posted: Fri Dec 14, 2012 8:13 am Post subject: RE:Teacher being weird? |
|
|
I really do not care about spelling on the internet, maybe a grammar nazi like you might, but it is the last thing on my mind when I am writing on the internet. |
|
|
|
|
|
Insectoid
|
Posted: Fri Dec 14, 2012 8:25 am Post subject: RE:Teacher being weird? |
|
|
Well, here's the thing. When your spelling and grammar are crap, you look really dumb. If you can't put in any effort to make your post readable, why should we put in any effort to help you? Reading poorly structured and punctuated posts takes a lot of energy that I'm not always willing to give. |
|
|
|
|
|
AntoxicatedDevil78
|
Posted: Fri Dec 14, 2012 9:58 am Post subject: RE:Teacher being weird? |
|
|
You will need to work on what you have shown poorly. |
|
|
|
|
|
Panphobia
|
Posted: Fri Dec 14, 2012 10:48 am Post subject: RE:Teacher being weird? |
|
|
I dont wanna be disrespectful but if you really cared about grammar you would not be a part of this site, Dan has very poor grammar, and none of you say anything. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Fri Dec 14, 2012 11:32 am Post subject: RE:Teacher being weird? |
|
|
Dan has dyslexia. |
|
|
|
|
|
Zren
|
Posted: Fri Dec 14, 2012 1:04 pm Post subject: RE:Teacher being weird? |
|
|
Do you spell check your resume? What about your English assignments? Your code represents you even more than that resume does (most likely). You don't write code for you to understand, you write code for others to read (like your teacher).
As for the matter of needing a method that will be called for use in a GUI:
code: | Comparator getComparator() {
switch (radioButtonField.getSelected().getValue()) {
switch "desc":
return Collections.reverseOrder();
switch "asc":
default:
return ...;
}
}
void quickSortButton_Click() {
quickSort(list, getComparator());
}
void quickSort(List<Object> list, Comparator comparator) {
}
|
Nothing is stopping you from having another function from calling another with baked in arguments.
One final thing I noticed, why are the algorithms of your bubble sort different? Ascending has an extra variable 'button' while descending does not. |
|
|
|
|
|
Panphobia
|
Posted: Fri Dec 14, 2012 1:13 pm Post subject: RE:Teacher being weird? |
|
|
I think I made an improvement on the bubble sort so it is more efficient and forgot to change the other, but that isnt what he said was wrong, he said the selection sort was wrong :S |
|
|
|
|
|
|
|