Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

Introduction

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.

Working of bubble sort algorithm:


Say we have an array unsorted A[0],A[1],A[2]................ A[n-1] and A[n] as input. Then the following
steps are followed by bubble sort algorithm to sort the values of an array.
1.Compare A[0] and A[1] .
2.If A[0]>A[1] then Swap A[0] and A[1].
3.Take next A[1] and A[2].
4.Comapre these values.
5.If A[1]>A[2] then swap A[1] and A[2]
...............................................................
................................................................
at last compare A[n-1] and A[n]. If A[n-1]>A[n] then swap A[n-1] and A[n]. As we see the highest
value is reached at nth position. At next iteration leave nth value. Then apply the same steps repeatedly
on A[0],A[1],A[2]................ A[n-1] elements repeatedly until the values of array is sorted.

In our example we are taking the following array values


12 9 4 99 120 1 3 10

The basic steps followed by algorithm:-

In the first step compare first two values 12 and 9.


12 9 4 99 120 1 3 10

As 12>9 then we have to swap these values


Then the new sequence will be
9 12 4 99 120 1 3 10

In next step take next two values 12 and 4


9 12 4 99 120 1 3 10

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.

The output of this example will be :


1 3 4 9 10 12 99 120

***********************************************************************************

/////////////////////////
//How to sort an array//
/////////////////////////

//This tutorial requies a basick undersatnding of java,


//arrays and for loops.

//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 :)

public class SortString


{
public static void sort(String words[]) //Sorting fuction, the array of nums is passed to it
{
String temp; //a temp var is need to swtich the values in the array

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.
}
}
}

public static void main(String[] args)


{
//declares an array to be sorted
String words[] = {"Hacker Dan", "Tony", "Aziz", "dark_knight_27", "Coutsos", "krishon",
"JayLo"};

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
}

sort(words); //sends the array to the sort() fuction

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]);
}
}
}

You might also like