//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;
}
}
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...
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 );
}
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?
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.
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];
}
comment about programsandeep April 2, 2011 at 8:26 PM
it's nice for sharing knowledge about programs
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!!
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?
program using buffered readermaithreyee March 25, 2012 at 5:44 PM
how do i write the same program using buffered reader?
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!
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