?
Solved

Question about SORT

Posted on 2003-03-11
7
Medium Priority
?
173 Views
Last Modified: 2010-03-31
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
Comment
Question by:Daving
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 2

Expert Comment

by:glottis
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

by:funnyveryfunny
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

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

bye
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Daving
ID: 8139962
Thank guys for the advice. Really appreciate this. Cheers.
0
 
LVL 4

Accepted Solution

by:
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

by:CleanupPing
ID: 9058770
Daving:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 35

Expert Comment

by:girionis
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

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month13 days, 5 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question