Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,481 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,781 people online right now. Registration is fast and FREE... Join Now!




Recursive Array Sorting Help

 

Recursive Array Sorting Help

zohoor

2 Jan, 2009 - 01:19 PM
Post #1

New D.I.C Head
*

Joined: 30 Dec, 2008
Posts: 1

hi how are u

i need to help plz my homwork it to sorting an arry whith recursion

in java and i rowht it but it dosnt work

CODE

import javax.swing.*;
import java.io.*;

public class asssaja {
       static int i=1;
      static int j=0;  
   static public void sorta(float a[],int x,int y)
   {
       float temp;
        if(j==y)
        return;
      if (a[x]>a[i])
       {
           temp=a[x];
           a[x]=a[i];
           a[i]=temp;
        }
      
    i++;
      sorta( a, j++, y);
        
   }
    
  
    public static void main(String args[])throws IOException{
     int arnum;
     String n,e;
    n= JOptionPane.showInputDialog("pleas enter the number element of array");
    arnum=Integer.parseInt(n);
    float []array=new float[arnum];
     for (int c=0;c<arnum;c++){
        e= JOptionPane.showInputDialog("pleas enter"+(c+1)+"element of array");
        array[c]=Float.parseFloat(e);
     }
    sorta(array,0,arnum-1);
    String output="the element of array by sorted\n";
    JTextArea t=new JTextArea();
    for (int c=0;c<arnum;c++)
        output+=array[c]+"\n ";
    t.setText(output);
    JOptionPane.showMessageDialog(null,t);
    System.exit(0);  
    }
    
  
}




pleas hary

thank you biggrin.gif

User is offlineProfile CardPM
+Quote Post


BetaWar

RE: Recursive Array Sorting Help

2 Jan, 2009 - 01:22 PM
Post #2

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 4,729



Thanked: 269 times
Dream Kudos: 1400
My Contributions
Renamed.
User is offlineProfile CardPM
+Quote Post

macosxnerd101

RE: Recursive Array Sorting Help

3 Jan, 2009 - 07:36 AM
Post #3

Self-Trained Economist
Group Icon

Joined: 27 Dec, 2008
Posts: 1,688



Thanked: 180 times
Dream Kudos: 325
Expert In: Java

My Contributions
It looks like you're trying to use Bubblesort recursively. Try Mergesort or Quicksort if you need to use recursion. Mergesort works by picking a pivot point and dividing the original collection into 2 sublists based on a comparison to the pivot point. Quicksort works by partitioning the collection based on the pivot point, eliminating the need to merge the sublists into one collection, making it more efficient than Mergesort. Both have a growth of O(n log n) though.


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:58AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month