Jump to content

search task

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
lmc059

lmc059

    Newbie

  • Members
  • PipPip
  • 16 posts
does anyone know where to start with this task im having some trouble getting to grips with java, i have uploaded the task.

Attached Files

  • Attached File  lab.doc   13.67K   44 downloads


#2
gszauer

gszauer

    Programmer

  • Members
  • PipPipPipPip
  • 113 posts
Not exactly what you need, but its close.
This is the sequential search code i use
[HIGHLIGHT="Java5"]public static int seqSearch (int[] list, int length, int search){
int loc;
for (loc = 0; loc < length; loc++)
if (list[loc] == search)
return loc;
return -1;
}[/HIGHLIGHT]

Tease might also come in handy
bubble sort:
[HIGHLIGHT="Java5"]public static void bubbleSort (int[] list, int length){
int temp, counter, index;

for (counter = 0; counter < length - 1; counter ++){
for (index = 0; index < length - 1 - counter; index++)
if (list[index] > list[index + 1]){
temp = list[index];
list[index] = list[index + 1];
list[index + 1] = temp;
}
}
}[/HIGHLIGHT]

Insertion Sort
[HIGHLIGHT="Java5"]public static void insertionSort (int[] list, int listLength){
int firstOutOfOrder, location, temp;
for (firstOutOfOrder = 1; firstOutOfOrder < listLength; firstOutOfOrder++)
if (list[firstOutOfOrder] < list[firstOutOfOrder - 1]){
temp = list[firstOutOfOrder];
location = firstOutOfOrder;
do {
list[location] = list[location - 1];
location --;
} while (location > 0 && list[location - 1] > temp);
list[location] = temp;
}
}[/HIGHLIGHT]

Selection Sort
[HIGHLIGHT="Java5"]public static void selectSort (int[] list, int length){
int index, sIntex, minIndex, temp;
for (index = 0; index < length - 1; index ++){
sIndex = index;
for (minIndex = index + 1; minIndex < length; minIndex++)
if (list[minIndex] < list[sIndex])
sIndex = minIndex;
temp = list[sIndex];
list[sIndex] = list[index];
list[index] = temp;
}
}[/HIGHLIGHT]

~Aristotle said:

It is the mark of an educated mind to entertain a tought without accepting it
If my post was helpful, please help me build some rep Posted Image

#3
Arkie

Arkie

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
From his attachment: Write a Java main method that calls the method.

Altough at first glance those methods will run, they are not recursive so i doubt he can turn that in.

That said, datastructures can be a pain if you dont know where to start. GL.

#4
gszauer

gszauer

    Programmer

  • Members
  • PipPipPipPip
  • 113 posts
Turning those in would be cheating, eh?

~Aristotle said:

It is the mark of an educated mind to entertain a tought without accepting it
If my post was helpful, please help me build some rep Posted Image

#5
Arkie

Arkie

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts

gszauer said:

Turning those in would be cheating, eh?

Trust me, when students get desperate they just turn in whatever that looks like their assignments.

That said, hmm college.. a few more months before getting my uni cs degree. :D