Home Discussion Discuss: Bubble Sorting in Java

  • QuestionRonel April 9, 2011 at 11:05 PM

    what is the purpose of the inner for loop?

  • Wrong statementAvi April 19, 2011 at 10:02 AM

    for(j = 1; j < (n-i); j++) this will not sort last element. for(j = 1; j < n; j++) This will fix issue. Enjoy!!

  • javaganesh August 20, 2011 at 6:04 PM

    nice explanation

  • Bug in the bubble sortMike Partyka September 13, 2011 at 4:19 AM

    The for loops are a little off: for(i = 0; i < n; i++){ for(j = 1; j < (n-i); j++){ should be: for(i = 0; i < (n-1); i++){ for(j = 1; j < n; j++){

  • Array sorting without any sorting techniquesmsr November 10, 2011 at 6:50 PM

    //sorting based on ascending and descending order based on asc or dsc passed in command prompt.... public class Sort2 { public static void main(String[] args) { //String order = args[0]; int[] a = stringToInt(args); int arrayLength=a.length; for(int i=0; i<arrayLength-1; i++) { if(args[0].equalsIgnoreCase("asc")) { int asc=returnMinIndex(a,i); a=swap(a, i, asc); } else if(args[0].equalsIgnoreCase("dsc")) { int dsc=returnMaxIndex(a,i); a=swap(a, i, dsc); } // for(int j = 0; j <a.length; j++) to see the sorted array // System.out.print(a[j]+" "); } for(int j = 0; j <a.length; j++) System.out.print(a[j]+" "); } //method to return Maximum Index Value public static int returnMaxIndex(int[] a, int startIndex) { System.out.println("startIndex is " +startIndex); int maxValue = a[startIndex]; int maxIndex = startIndex; for (int i = startIndex+1; i < a.length; i++) { if (a[i] > maxValue) { maxValue = a[i]; maxIndex = i; } } System.out.println("maxIndex=" +maxIndex); return maxIndex; } //method to return minimum Index value public static int returnMinIndex(int[] a, int startIndex) { System.out.println("startIndex is " +startIndex); int minValue = a[startIndex]; int minIndex = startIndex; for (int i = startIndex+1; i < a.length; i++) { if (a[i] < minValue) { minValue = a[i]; minIndex = i; } } System.out.println("minIndex=" +minIndex); return minIndex; } //method to swap to elements public static int[] swap(int a[],int minIndex , int maxIndex ) { int t=0; t= a[minIndex]; a[minIndex]=a[maxIndex]; a[maxIndex]=t; return a; } //method to convert string array to int array public static int[] stringToInt(String[] args) { int arr[]= new int[args.length-1]; for(int i= 0; i<args.length-1; i++) { arr[i] = Integer.parseInt(args[i+1]); //args[i+1]--- to ignore asc or dsc } return arr; } }

  • About the programNiladri Dutta November 24, 2011 at 10:26 AM

    Excellent thank you so much you helped me so much i cant xpress it in words...........!!!!!!!!!!!

  • ThanksSidharth Sahoo February 19, 2012 at 9:04 AM

    Thanks a lot.. I am a student of Class XI of ISC Course.. I had forgotten the syntax of bubble sorting.. We did it in Class X but I had lost my notes... Your website was of great help to me before my exams...

  • Proper code for bubble sortMichal March 8, 2012 at 2:41 AM

    The algoritthm presented here is not a bubble sort. The proper code (swap just swaps two elements in array) should be: private static void bubbleSort(int array[]){ int swaps = 0; do{ swaps = 0; for(int i = 1; i < array.length; i++){ if( array[i] < array[i-1]){ swaps++; swap(array, i, i-1); } } }while( swaps != 0 ); }

  • Bubble SortAnonymous April 27, 2012 at 1:02 AM

    How can I modify your code so that we can have different input sizes such as 10,000, and 10,000,000, and then output the time that they finish running bubble sort using various input sizes?

  • javamamta June 6, 2012 at 2:21 PM

    very nice

  • computerpuja June 17, 2012 at 12:34 PM

    get lost

  • To get Job as Java Developerrajesh yadav August 15, 2012 at 2:45 PM

    i am begainer of software industries(sply in java ) my programing skills is not so good so how we improve my skills?to survive in software industries????.I am 2010 passout B.Tech (ECE)student.

  • sorting programmerinku August 16, 2012 at 8:04 PM

    the given sorting programme is really easy.thanks

  • to improve bubbulesort ExampleVishal September 18, 2012 at 4:36 PM

    you r change the value from two place like insertion sort so there is change small part of code. public static void bubble_srt( int a[], int n ){ int i, j,t=0; for(i = 0; i < n; i++){ for(j = 1; j < (n-i); j++){ if(a[j-1] > a[j]){ a[j]=a[j]+a[j-1]; a[j-1]=a[j]-a[j-1]; a[j]=a[j]-a[j-1]; }

  • programmingjollibee December 13, 2012 at 2:29 PM

    what is the best technique in programming?

  • cs125(descrete math)nilo botay September 30, 2012 at 9:12 PM

    your code is very good you know?i almost understand it but i do not..paglung-ag sa sa sunod para dili ta manga pasmo huh??

  • core javapooja nakil October 18, 2012 at 11:21 AM

    easy to understand nice...............

  • Bubble sort Gamer December 30, 2012 at 11:19 PM

    WHy can't we do bubble sort with only one for loop ??

  • HiAlvin December 4, 2012 at 11:25 AM

    Nice!

  • My Version of Bubble SortBryan Hayag June 20, 2013 at 12:45 PM

    //You can try this Code if this fits your needs... import java.io.*; import java.util.*; public class BubbleSort { public static void main(String args[]) throws IOException { int i; Scanner in = new Scanner(System.in); System.out.println("Please enter number of items to Sort:"); int nItems = in.nextInt(); int[] ax = new int[nItems]; for (i=0;i<nItems;i++) { System.out.print("\nEnter a Number:"); ax[i]=in.nextInt(); } bubble_srt(ax, ax.length); System.out.print("\nThe values after sort: \n"); for (i=0; i< ax.length; i++) { System.out.print(ax[i]+" "); } System.out.println(); System.out.println("\nPAUSE..."); } public static void bubble_srt(int a[], int n) { int i,j,t=0; for (i=0; i<n; i++) { for (j=1; j<(n-i); j++) { if (a[j-1] > a[j]) { t=a[j-1]; a[j-1]=a[j]; a[j]=t; } } } } }

  • data structureaki claks June 30, 2013 at 9:33 AM

    thanks for the codes, it helps a lot but how can i convert it into (GUI)Graphical User Interface?

  • MY COOOOOOOOMENT !JV a'k'a ( Lee Min Ho ) March 19, 2014 at 6:22 AM

    All i can say about the program is AWESOME ! :)

  • Wrong statementNiladri Dutta November 24, 2011 at 10:28 AM

    Objection avi the actual statement is for(j=0;j<n-i-1;j++)

Post your Comment



Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Related Articles
Java Beginners

Sorting
Sorting  can any help me know which sorting algorithm java uses for sorting collection and arrays 

Java Beginners

Sorting in Java
Sorting in Java  Sorting in Java 

Java Beginners

Java sorting
the following links: Java bubble sort Java Heap Sort Java Insertion Sort Java Merge Sort Java Selection Sort Java Quick Sort   hello...Java sorting  can somebody help me know how java sorts? means which 

Java Beginners

Bubble Sorting in Java
Bubble Sorting in Java  Hi, What is Bubble Sorting? Guide me where to learn Bubble Sorting in Java? Thanks   Hi, Check the tutorial at Bubble Sorting in Java. Thanks 

Java Beginners

Java Sorting
Java Sorting  Could somebody help me with this question. Implement a program to process votes for 5 candidates in a talent contest. The program should use a String array to hold the names of the 5 candidates and an integer 

Java Interview Questions

Sorting and Searching
Sorting and Searching  Hi i would really be thankful if someone could help me with this A program is required to ask users to rate the Java...) { Scanner input = new Scanner(System.in); System.out.print("Rate Java(0-10 

Java Beginners

Sorting an ArrayList
Sorting an ArrayList  print("code sample");Hello All, I am working on a program for school and I am having trouble with sorting my list... really new at Java...This is only the second program I am writing for school 

JavaScript Questions

sorting array in java
sorting array in java  How to sort array in Java or JavaScript?   JavaScript Sorting array tutorial   Java Sort array of strings import java.util.*; class ArrayExample{ public static void main(String 

Java Beginners

Sorting in java - Java Beginners
Sorting in java  Hello.. I want you to help me with this question.. 1. A statistics company wants to keep information of families. The information of a family is the family name, the number of members and first name of each 

Java Beginners

Sorting algorithms - Java Beginners
Sorting algorithms  I'v being given an assignment and I still couldn't find a proper answer for the following questions.Can anyone pls pls help me wi it? 1)Compare and contrast efficiencies of Shell,Quick,Heap and Radix sort 

Java Beginners

Java Sorting and Searching
Java Sorting and Searching   If anyone could help me with this I would be really thankful! Write a program that stores the names of these artists in a String array. The program should prompt the user to enter the name 

Java Interview Questions

SORTING - Java Interview Questions
("Items after sorting are ="+list); } } Thanks 

Java Beginners

java sorting codes - Java Beginners
java sorting codes  I want javasorting codes. please be kind enogh... the following link: http://www.roseindia.net/java/beginners/arrayexamples/ Here you will get various sorting algorithms. Hope that it will be helpful for you 

Java Beginners

java array sorting manual way
java array sorting manual way  Hi, Anyone has any sample code on how to sort a array manually without using the inbuild sorting algorithm? Thanks 

Java Beginners

merge sorting in arrays - Java Beginners
merge sorting in arrays  Write a program to insert string or characters to an array and apply merge sorting on this array  Hi Friend, Please visit the following link: http://www.roseindia.net/java/beginners 

Java Beginners

sorting - Java Beginners
; Easy Sorting: For all the values in the array A, find the largest and store 

IDE Questions

sorting
sorting   write a program to arrange sorting in rows,column and diagonal 

Java Beginners

Array sorting - Java Beginners
Array sorting   Hello All. I need to sort one array based on the arrangement of another array. I fetch two arrays from somewhere and they are related. For example, String type[] = {"X","T","3","R","9"}; String 

Java Beginners

Java Hashmap Sorting - Java Beginners
Java Hashmap Sorting   I have a sorting issue with a Hashmap. My constraint is that I MUST use the Hashmap and work with existing code. I do..., it loses the original alphabetical sorting done by the database. So, my problem 

Java Beginners

Sorting Router Interface in a list.
, GigabitEthernet 1/11 How can i achieve this type of sorting in java. It's really urgent...Sorting Router Interface in a list.  Hi All, I have list which contains router interface as GigabitEthernet 1/0, GigabitEthernet 1/1,GigabitEthernet 

Java Beginners

sorting
sorting  write to a program to arrange the sorting in rows, column and digonal 

Java Beginners

Comparison between the types of sorting in java
Comparison between the types of sorting in java  welcome all i wanna program in java compare between selection,insertion,bubble,merge,quick sort... of sorting. please help me thanks all 

Java Beginners

sorting
sorting  how to do sorting without using bubble sort,selection sort 

Java Beginners

Alphabetically sorting order
Alphabetically sorting order  Write a java program that takes a list of words from the command line and prints out the arguments in an alphabetically sorted order   Hi Friend, Try the following code: import 

Java Beginners

NAME SORTING. . .anyone? - Java Beginners
NAME SORTING. . .anyone?  how can I sort names without using the 'name.sort' method? please help. . .anyone? the program should sort the first three(3) letters of the names tnx java masters out there!! (^_^) cVm  Hi 

XML Questions

Sorting Xml file - XML
Sorting Xml file  I have an xml file like this: Smith USA Bob USA John UK Smith SG Amy AUC And I would... Smith USA SG I will do my implementation in Java. So, I would 

Java Beginners

sorting an array of string with duplicate values - Java Beginners
sorting an array of string  Example to sort array string 

Java Beginners

sorting student record - Java Beginners
sorting student record  Program in java for inserting, recording, deleting, editing and searching student details   can u explain about recording ? u want to store value in database or in file or opertinng run time 

Java Beginners

sorting
 

Java Beginners

sorting
 

DMCA.com