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 2 of 2

Thread: HELP ME PLEASE, i cant figure it out whats the problem of Bubble & Selection sor :(

  1. #1
    Junior Member
    Join Date
    Dec 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy HELP ME PLEASE, i cant figure it out whats the problem of Bubble & Selection sor :(

    import java.util.*;

    public class MidtermNina
    {
    public static void main(String[] args)
    {
    Scanner console = new Scanner(System.in);
    Scanner call = new Scanner(System.in);

    System.out.println("INPUT 5 RANDOM INTEGERS: ");

    int[] arr = new int[5];
    for(int i=0; i<arr.length; i++)
    {
    arr[i] = console.nextInt();
    }

    System.out.println("SELECT SORTING METHOD: 1. BUBBLE SORT, 2.SELECTION SORT, 3.INSERTION SORT");
    int sortChoice = call.nextInt();

    switch(sortChoice)
    {
    case 1:
    bubbleSorts(arr);
    break;
    case 2:
    selectionSorts(arr);
    break;
    case 3:
    insertionSorts(arr);
    break;
    default:
    System.out.print("NUMBER IS NOT IN THE CHOICES! ");
    break;

    }

    }
    public static void printArray(int[] arr)
    {
    for(int z=0; z<arr.length;z++)
    {
    System.out.print(" ");
    System.out.print(arr[z]);
    }
    System.out.println();

    }

    public static void bubbleSorts(int[] arr)
    {
    int count=0;
    int count1=0;
    int count2=0;

    for(int i=1; i<arr.length-1; i++)
    {
    count++;
    for(int j=0;j<arr.length-1;j++)
    {
    count1++;

    if(arr[j] > arr[j+1])
    {
    count2++;
    int temp = arr[j];
    arr[j] = arr[j+1];
    arr[j+1] = temp;
    }
    }
    System.out.print("BUBBLE SORTING"+i+":");
    printArray(arr);
    }
    System.out.println("");
    System.out.println("Number of Reset:"+count);
    System.out.println("Comparison: "+count1);
    System.out.println("Swap: "+count2);

    System.out.print("Bubble Sorted Integers: ");

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

    public static void selectionSorts(int[] arr)
    {
    int reset =0;
    int counts =0;
    int swap =0;

    for(int i=0; i<arr.length-1;i++)
    {
    reset--;

    int index=i;

    for(int j=i+1;j<5;j++)
    {
    counts++;

    if(arr[j] < arr[index])
    {
    swap++;

    index = j;
    }

    }
    int temp = arr[index];
    arr[index] = arr[i];
    arr[i] = temp;

    System.out.print("Doing Sorting Pass: "+i+": ");
    printArray(arr);
    }

    System.out.println();
    System.out.println("Number of Reset: "+reset);
    System.out.println("Number Comparison: "+counts);
    System.out.println("Number Swap: "+swap);

    System.out.print("Selection Sorting: ");

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

    }


    public static void insertionSorts(int[] arr)
    {
    int reset=0;
    int count=0;
    int swap=0;

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

    while(j>=0 && arr[j] > temp)
    {
    count++;
    arr[j+1] = arr[j];
    j = j-1;
    }
    arr[j+1] = temp;
    swap++;

    System.out.print("INSERTION SORTING: "+i+": ");
    printArray(arr);
    }
    System.out.println();
    System.out.println("Number of Reset: "+reset);
    System.out.println("Comparison : "+count);
    System.out.println("Swap: "+swap);

    System.out.print("Insertion Sorting: ");

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

    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HELP ME PLEASE, i cant figure it out whats the problem of Bubble & Selection sor :(

    Can you copy the program's output and paste it here so we can see what it is doing?
    Add some comments to show what is wrong.

    Also add some comments to the code describing what it is trying to do and how it is going to do it.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. DATA STRECTURES - SORTING - BUBBLE-INSERTION-SELECTION SORT IN JAVA
    By itscnumca in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 27th, 2021, 05:02 AM
  2. Sorting: Selection, insertion, bubble
    By NorrinGalan in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 11th, 2014, 07:32 AM
  3. Sorting: Selection, insertion, bubble
    By NorrinGalan in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 10th, 2014, 08:10 PM
  4. can't figure whats wrong with my add method?? help!
    By b094mph in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 8th, 2011, 05:00 PM
  5. bubble sort and selection sort on strings
    By Sir Saula in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2010, 09:44 AM