Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

Posted on 2003-03-11
Medium Priority
175 Views
Hi. I'm new on java, and I need to know. What is a pseudocode for the SORTER method?

Below is what I got so far (actually it's a "sorting routine"):

public void program( String args[] )
{
String names[100];

bubble_sort( num, names );
}

void bubble_sort( int nr, String list[] )
{

}
0
Question by:Daving

LVL 2

Expert Comment

ID: 8114389
you can use Arrays.sort method in the java.util package.
<snip comment>
The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist).  This algorithm offers guaranteed n*log(n) performance.
</snip>

If you are really looking for bubble sort then look into the demo\applets\SortDemo folder of you JDK installation directory. It contains the BubbleSortAlgorithm which you may use
0

LVL 4

Expert Comment

ID: 8139191
The bubble sort needs two pointers, one for scanning through the A array n times where n is the number of items in an array. The Other is for comparing two adjacent items in an array, i.e A[j]<=A[j+1] where j is the number of items-1. So overall this gives rise to two nested loops. The psuedo code is as follow:

while(i<n){
while(j<n-1){
if(A[j]>A[j+1]) swap;
j++;
}
i++;
}

If you're still stuck here is the code below, but try to do it yourself first that way you will understand better. Have fun and maybe try you should try to implement QuickSort after this - excellent source for Recursive techniques.

This is the code for bubble sort:

public void bubbleSort(int[] data){
for(int i=0;i<data.length;i++)
for(int j=0;j<data.length-1;j++){
if(data[j]>data[j+1]){
int temp = data[j+1];
data[j+1]=data[j];
data[j]=temp;
}
}
}
0

LVL 4

Expert Comment

ID: 8139226
I have shown a numerical implemetation of bubble sort, you can adapt it to sorting string[].

bye
0

Author Comment

ID: 8139962
Thank guys for the advice. Really appreciate this. Cheers.
0

LVL 4

Accepted Solution

funnyveryfunny earned 200 total points
ID: 8140228
Since I've shown in another question how string data can be sorted, might just well stick it here for yr reference. I'm not sure if it's a best way of doing string comparision haven't tested my idea sufficiently yet but anyway here is the code:

public class BubbleSort {

public BubbleSort() {
}
public static void main(String[] args) {
BubbleSort b = new BubbleSort();
String[] data ={"def","ghi","abc","jkl"};

System.out.println("==========Before===========");
for(int i=0;i<data.length;i++)
System.out.println(data[i]);

b.bubbleSort(data);

System.out.println("===============After==============");
for(int i=0;i<data.length;i++)
System.out.println(data[i]);
}

private boolean smallerThan(String s, String s2){
byte[] s2b = s2.getBytes();
byte[] sb = s.getBytes();
int len=0;
boolean s_lessThan_s2=false;
if(sb.length>s2b.length)
len = s2b.length;
else
len = sb.length;
for(int i=0;i<len;i++){
if(sb[i]<s2b[i]){
s_lessThan_s2 = true;
break;
}
}
return s_lessThan_s2;
}

public void bubbleSort(String[] data){
for(int i=0;i<data.length;i++)
for(int j=0;j<data.length-1;j++){
if(!smallerThan(data[j],data[j+1])){
String temp = data[j+1];
data[j+1]=data[j];
data[j]=temp;
}
}
}
}
0

Expert Comment

ID: 9058770
Daving:
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

LVL 35

Expert Comment

ID: 9700672
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to funnyveryfunny

girionis
EE Cleanup Volunteer
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
###### Suggested Courses
Course of the Month13 days, 4 hours left to enroll