Results 1 to 12 of 12
  1. #1
    moni sharma is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Post problem related to array ..........

    hello every one...
    i need a little help regarding my homework in java,
    its a merge sort program.i want to modify it so that it can sort both the integers and strings both .but string array is not working for integers as its considering integer as string so its sorting like:
    21 12 23 3
    sorted values:12 ,21,23,3
    i think now u got my point what i want to say.
    put do some thing with this code so that it can take input as integer and string both and sort them properly.
    plz help me i have just 2 dayz for submission of this work.....


    import java.util.*;

    public class MergeSort{
    public static void main(String a[]){
    int i;
    int array[] =new int[5];

    Scanner input=new Scanner(System.in);

    System.out.println(" MergeSort\n\n");
    System.out.println("Values Before the sort:\n");
    for(i = 0; i < array.length; i++)
    {
    array[i]=input.nextInt();
    }
    System.out.println();
    mergeSort_srt(array,0, array.length-1);
    System.out.print("Values after the sort:\n");
    for(i = 0; i <array.length; i++)
    System.out.print(array[i]+" ");
    System.out.println();
    System.out.println("PAUSE");
    }

    public static void mergeSort_srt(int array[],int lo, int n){
    int low = lo;
    int high = n;
    if (low >= high) {
    return;
    }

    int middle = (low + high) / 2;
    mergeSort_srt(array, low, middle);
    mergeSort_srt(array, middle + 1, high);
    int end_low = middle;
    int start_high = middle + 1;
    while ((lo <= end_low) && (start_high <= high)) {
    if (array[low] < array[start_high]) {
    low++;
    } else {
    int Temp = array[start_high];
    for (int k = start_high- 1; k >= low; k--) {
    array[k+1] = array[k];
    }
    array[low] = Temp;
    low++;
    end_low++;
    start_high++;
    }
    }
    }
    }

  2. #2
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Hi Moni - first of all lets begin by putting code tags for your program so it will be easy on everyone's eyes.

    Secondly - can you please provide some of the rules for this program. Does the user get to choose whether to sort integers or strings?

    Reposting OP code with tags:

    Java Code:
    import java.util.*;
    
    public class MergeSort {
    	public static void main(String a[]) {
    		int i;
    		int array[] = new int[5];
    
    		Scanner input = new Scanner(System.in);
    
    		System.out.println(" MergeSort\n\n");
    		System.out.println("Values Before the sort:\n");
    		for (i = 0; i < array.length; i++) {
    			array[i] = input.nextInt();
    		}
    		System.out.println();
    		mergeSort_srt(array, 0, array.length - 1);
    		System.out.print("Values after the sort:\n");
    		for (i = 0; i < array.length; i++)
    			System.out.print(array[i] + " ");
    		System.out.println();
    		System.out.println("PAUSE");
    	}
    
    	public static void mergeSort_srt(int array[], int lo, int n) {
    		int low = lo;
    		int high = n;
    		if (low >= high) {
    			return;
    		}
    
    		int middle = (low + high) / 2;
    		mergeSort_srt(array, low, middle);
    		mergeSort_srt(array, middle + 1, high);
    		int end_low = middle;
    		int start_high = middle + 1;
    		while ((lo <= end_low) && (start_high <= high)) {
    			if (array[low] < array[start_high]) {
    				low++;
    			} else {
    				int Temp = array[start_high];
    				for (int k = start_high - 1; k >= low; k--) {
    					array[k + 1] = array[k];
    				}
    				array[low] = Temp;
    				low++;
    				end_low++;
    				start_high++;
    			}
    		}
    	}
    }
    --user0--

  3. #3
    moni sharma is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    "yes ,user has a option to choose either integers or String........."

  4. #4
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    How do u plan to sort the String or more precisely how exactly u want to sort the String ???? any example
    _______________________________________________
    give me beans .........

  5. #5
    moni sharma is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Post

    for example:
    value before sort: jon,adom,anne ,moni,tina
    sorted vale;
    adom,anne,jon, moni,tina


    and also applicable for numbers:

    value before sort: 123 ,11,33,5,3
    sorted value: 3,5,11,33,123

    plz modify it by any how.use whatever the conditions u want to apply.but in the end code must be capable to sort both strings and integers value......
    plz try to solve..........................

  6. #6
    moni sharma is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    by string i mean, character string and integer string both......i think now u got my point.....

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Why don't you simply use List and Arrays.

    For Strings: Store your String values in list [ArrayList] and then sort them by using Collections.sort(List<T> list)

    For Integers: Store your integer values in Array [Plain int array] and then sort them by using Arrays.sort(int[] a)

    Will that be enough for you?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by moni sharma View Post
    by string i mean, character string and integer string both......i think now u got my point.....
    Build a little class with the following methods:

    Java Code:
    public class C { 
       ...
       public int getAsInt() { ... }
       public String getAsString() { ... }
    }
    Those two methods return the representation of the object as an int or a String respectively. Next implement two Comparators<C> classes; the first one uses the getAsInt() method for its comparison, the other one uses the getAsString() method. You can use the built-in Collections.sort( ... ) method or the Arrays.sort( ... ) method given the Comparator<C> objects.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Join Date
    Jul 2010
    Posts
    12
    Rep Power
    0

    Default

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;

    /**
    *
    * @author AnanthaNag.KUNDANALA
    */



    public class MergeSort {

    private boolean end = false;
    List<String> stringList = new ArrayList<String>();
    List integersList = new ArrayList();
    String[] st = null;
    List finalList = new ArrayList();

    private void askInputs() throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("press * to stop entering the inputs .");
    while (!end) {
    String temp = br.readLine();
    try {
    int temptry = Integer.parseInt(temp);
    integersList.add(temptry);
    System.out.print(" --- > Number ");
    } catch (NumberFormatException ex) {
    if (temp.equalsIgnoreCase("*")) {
    end = true;
    break;
    } else {
    stringList.add(temp);
    System.out.print(" --- > String ");
    }
    }
    System.out.println("");
    }
    }

    public static void main(String argc[]) throws IOException {
    MergeSort sort = new MergeSort();
    sort.askInputs();
    sort.sortIntegers();
    sort.sortStrings();
    sort.display();
    }

    private void sortStrings() {
    int len = stringList.size();
    for (int i = 0; i < (len - 1); i++) {
    for (int j = (i + 1); j < len; j++) {
    String temp1 = (String) stringList.get(i);
    String temp2 = (String) stringList.get(j);
    if (temp1.compareToIgnoreCase(temp2) > 0) {

    stringList.remove(i);

    stringList.add(i, temp2);
    stringList.remove(j);
    stringList.add(j, temp1);
    }
    }
    }

    finalList.addAll(stringList);

    }

    private void sortIntegers() {
    int len = integersList.size();
    for (int i = 0; i < (len - 1); i++) {
    for (int j = (i + 1); j < len; j++) {
    int temp1 = (Integer) integersList.get(i);
    int temp2 = (Integer) integersList.get(j);
    if (temp1 > temp2) {
    integersList.remove(i);
    integersList.add(i, temp2);
    integersList.remove(j);
    integersList.add(j, temp1);
    }
    }
    }

    finalList.addAll(integersList);
    }

    private void display() {
    Iterator iterator = finalList.iterator();
    while (iterator.hasNext()) {
    System.out.println(iterator.next());
    }
    }
    }



    Hi moni,

    this will be your exact required programme...

    Through this , you can directly enter both the string and int values aswell at a time ....

    Both of them can sort and will display ...

    enjoy ..

    For any inf ..

    dont hesitate to ask ...

    Have a nice day ...

  10. #10
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Hey AnanthNag,

    1) Use code tags when you post your code here.

    2) Don't provide the entire solutions for any posts.

    3) If you are so eager to solve assignments, do it in your own workspace. But when you post here, provide only pointers by which the OP can get to the destination.

    4) Spoon feeding is not the culture here.

    You have done good, but unfortunately in the wrong way. I hope you will understand and avoid any such things in future.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  11. #11
    Join Date
    Jul 2010
    Posts
    12
    Rep Power
    0

    Default ok goldest.

    Hey ,

    It will not repeat again .

    As I am new guy to the forum , I don't know all this stuff..

    Any way your words received positively and will follow the way you suggested.

    Ananth.

  12. #12
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Thank you very much! :D
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Very Strange Problem related To GUI
    By ravjot28 in forum AWT / Swing
    Replies: 8
    Last Post: 05-18-2010, 04:06 PM
  2. problem related to custom tags in jsp
    By garima in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-20-2009, 01:04 PM
  3. Problem related to WebService
    By keshari in forum Web Frameworks
    Replies: 0
    Last Post: 05-23-2008, 08:50 AM
  4. Math Related Problem
    By perito in forum Advanced Java
    Replies: 1
    Last Post: 03-21-2008, 09:53 AM
  5. Math Related Problem
    By perito in forum New To Java
    Replies: 3
    Last Post: 03-20-2008, 06:22 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •