Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: I need help with this array

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need help with this array

    My program is about generating random numbers in an array and sort them.
    I am wishing that i could do bubble sort ,selection sort, insertion sort, and shell sort in this program. Its okay if i had to redo the whole program just as long as you guys help me. I need to show the output of the bubble sort, selection sort, insertion sort, and shell sort individually, as i would be comparing their run time in sorting.




    import java.util.Arrays;
    import java.util.*;

    public class Sorting {

    public static void main(String[] args) {
    Random randomGenerator = new Random();
    for (int idx = 1; idx <= 10; ++idx) {
    int arrayList[] = {randomGenerator.nextInt(100)};
    int i;

    for (i = 0; i < arrayList.length; i++) {
    System.out.print(arrayList[i] + " ");
    }
    System.out.println();


    }
    }
    }



    I am to much of a beginner to figure it out my self


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    586
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: I need help with this array

    Please use code highlighting, that is not readable.

    I don't see any sorting in your code. What's your problem?

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help with this array

    sorry i am a beginner in java programming and im not familiar with highlighting
    i need to sort the following code using bubble,selection,insertion,and shell sort. I cant seem to do any of the following sorting methods as i am very clueless on how to insert their java code, please help. I just need to sort them and show the sorted array.

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    586
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: I need help with this array

    Nobody here will do it for you. Read wikipedia about the sorting alogorithms and post your best attempt and come back with specific questions.
    In the various announcements and in my signature you can read about code highlighting.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,537
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default Re: I need help with this array

    int arrayList[] = {randomGenerator.nextInt(100)};
    What is that line supposed to do? Are you trying to create an array? If so, that is not correct as the nextInt method returns a single int and you are trying to assign it to a variable that should reference an array. Also why are you doing it inside the loop. This means a new array will be created each time aroundd the loop. I imagine you only want to create the array once.

    Another thing it appears you have the second loop inside the first. This means that each time around the loop you will print out the entire contents of the array. I imagine you will want to print the array once after it has been sorted.

    Plenty to fix before you even think about sorting.
    Improving the world one idiot at a time!

  6. #6
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help with this array

    import java.util.*;

    public class Sorting {
    private static int temp;

    public static void main(String[] args) {

    Random randNumGenerator = new Random();
    int[] arrayList = new int[10];
    for (int i = 0; i < arrayList.length; i++) {
    arrayList[i] = (randNumGenerator.nextInt(100) + 1);
    }
    for (int i = 0; i < arrayList.length; i++) {
    System.out.print(arrayList[i] + " ");
    }
    System.out.println();
    System.out.println(Arrays.toString(CrunchifyBubble SortAsceMethod(arrayList)));



    for (int i = 0; i < arrayList.length - 1; i++) {
    int minIndex = i;
    for (int j = i + 1; j < arrayList.length; j++) {
    if (arrayList[minIndex] > arrayList[j]) {
    minIndex = j;
    }
    }
    if (minIndex != i) {

    int temp = arrayList[i];
    arrayList[i] = arrayList[minIndex];
    arrayList[minIndex] = temp;
    }
    }

    for (int i = 0; i < arrayList.length; i++) {
    System.out.print(arrayList[i] + " ");

    }
    System.out.println();
    for (int i = 1; i < arrayList.length; i++) {
    for (int j = i; j > 0; j--) {
    if (arrayList[j] < arrayList[j - 1]) {
    temp = arrayList[j];
    arrayList[j] = arrayList[j - 1];
    arrayList[j - 1] = temp;
    }
    }
    }

    for (int i = 0; i < arrayList.length; i++) {
    System.out.print(arrayList[i] + " ");

    }
    System.out.println();



    }


    public static int[] CrunchifyBubbleSortAsceMethod(int[] arr) {
    int temp;
    for (int i = 0; i < arr.length - 1; i++) {

    for (int j = 1; j < arr.length - i; j++) {
    if (arr[j - 1] > arr[j]) {
    temp = arr[j - 1];
    arr[j - 1] = arr[j];
    arr[j] = temp;
    }
    }

    }
    return arr;
    }
    }

    how bout now?
    i just need a short sorting algortihm, any suggestions?

  7. #7
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    586
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: I need help with this array

    Please use code highlighting, that is not readable.

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: I need help with this array

    Quote Originally Posted by Yutenio View Post
    sorry i am a beginner in java programming and im not familiar with highlighting
    Hi Yutenio and welcome to the forum.
    Please see the Announcements page for help with code tags.

    Quote Originally Posted by Yutenio View Post
    i just need a short sorting algortihm, any suggestions?
    A search engine is the place to find things. This is a place to ask questions.
    Looks like you come prepared with your own set of search keywords: bubble,selection,insertion,and shell sort
    See what you can find and ask a question when you get stuck.

Similar Threads

  1. Java 1.4 Array issues - controlling array size for dynamic usage
    By doonan79 in forum Collections and Generics
    Replies: 5
    Last Post: June 18th, 2013, 11:53 AM
  2. compile errors when creating a 2nd array the same size as 1st array
    By javaiscool in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2013, 09:35 PM
  3. Replies: 2
    Last Post: January 14th, 2013, 02:22 PM
  4. Doubling The Array Size And Randomizing Array Return
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 27th, 2011, 10:50 AM
  5. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM

Tags for this Thread