Professional Documents
Culture Documents
Shorting
Shorting
In this example we are going to sort integer values of an array using bubble sort.
Bubble sort is also known as exchange sort. Bubble sort is a simplest sorting algorithm. In bubble sort
algorithm array is traversed from 0 to the length-1 index of the array and compared one element to the
next element and swap values in between if the next element is less than the previous element. In other
words, bubble sorting algorithm compare two values and put the largest value at largest index. The
algorithm follow the same steps repeatedly until the values of array is sorted. In worst-case the
complexity of bubble sort is O(n2) and in best-case the complexity of bubble sort is Ω(n).
Code description:
Bubble Sorting is an algorithm in which we are comparing first two values and put the larger one at
higher index. Then we take next two values compare these values and place larger value at higher
index. This process do iteratively until the largest value is not reached at last index. Then start again
from zero index up to n-1 index. The algorithm follows the same steps iteratively unlit elements are not
sorted.
Compare these two values .As 12>4 then we have to swap these values.
Then the new sequence will be
9 4 12 99 120 1 3 10
We have to follow similar steps up to end of array. e.g.
9 4 12 99 120 1 3 10
9 4 12 99 120 1 3 10
9 4 12 99 1 120 3 10
9 4 12 99 1 120 3 10
9 4 12 99 1 3 120 10
9 4 12 99 1 3 10 120
When we reached at last index .Then restart same steps unlit the data is not sorted.
***********************************************************************************
/////////////////////////
//How to sort an array//
/////////////////////////
//To start you will need to know how to swicth the values
//of two vars. this ushley requires and a temp var to hold
//the value of one of them while they are being swiched.
//Ex:
//
//int myVarOne = 10;
//int myVarTwo = 20;
//int temp;
//
//temp = myVarOne;
//myVarOne = myVarTwo;
//myVarTwo = temp;
//
//now the value of myVarOne is in myVarTwo and
//the value of myVarTwo is in myVarOne
//Note:
//The flowing exampe of bubble sorting can be used with
//any array of a var type that can be compared with an
//boolean oprator.
//
//Also this example list the array in order of bigest value
//to smallest value. to chage this you must chage the if
//stament in the seconded for loop.
//Edit by Aziz:
//Changed to sort strings. Just fooling around. All credit to da Dan :)
for(int i = 0; i < words.length - 1; i++) //Start of the bubble sort, you need to take one away
from
{ //the legnth of the array b/c you dont need to go throw the
for(int j = 0; j < words.length - 1; j++) //array that many times. Also if it whould go passt the
end of
{ //the array in the second loop wich whould not been good.
if(words[j].compareTo(words[j+1]) > 0) //This part checks to see if the number
before it is bigger
//You must use the compareTo() method of a string
{ //Note: chage the < opprator to > to sort from samllest to lagergets
temp = words[j]; //Sotres the value of nums[ii] in the temp var for use latter
words[j] = words[j+1]; //puts the value of the bigger number where the lesser one
was
words[j+1] = temp; //puts the value of the lesser var back in the array where
the
} //biger one was.
}
}
}
System.out.println("Before sort():\n");
for(int i = 0; i < words.length; i++) //for loop to show all the values of the array
{
System.out.println(words[i]); //uses the for loop index var to slect an array entreay
}
System.out.println("\nAfter sort():\n");
for(int i = 0; i < words.length; i++) //for loop to show the realostes of the sorting
{
System.out.println(words[i]);
}
}
}