1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Bubble Sorting

Discussion in 'Databases' started by honeyclarck, Apr 13, 2012.

  1. #1
    I know what is the meaning of sort. But recently i hear the Bubble sort. So tell me please what is Bubble Sort?
     
    honeyclarck, Apr 13, 2012 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    plog, Apr 13, 2012 IP
  3. mikeweller

    mikeweller Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    3
    Trophy Points:
    0
    #3
    Read the Wikipedia page for "Bubble Sort".

    But generally, you don't need to know how the sorting is done. Just use "order by" in your SQL and the database will do the sorting for you.
     
    mikeweller, Apr 14, 2012 IP
  4. Decode141

    Decode141 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    So, basically if you want to sort 4,3,2,1 using bubble sort, you'd do:
    Compare 4 with 3, if 4 is larger we swap. We repeat this till 4 is compared with every other element. So...
    3|4|2|1 Swapped 3 and 4
    3|2|4|1 Swapped 2 and 4
    3|2|1|4 Swapped 1 and 4

    Now we do the same for 3.
    2|3|1|4 Swap 2 and 3
    2|1|3|4 Swap 3 and 1
    2|1|3|4 Since 3 is not greater than 4, we don't swap.

    Lastly we do this for 2.
    After this we get 1|2|3|4

    It's rather slow and most databases use better sorting algorithms like Quick Sort or Merge Sort.
     
    Decode141, Apr 15, 2012 IP
  5. honeyclarck

    honeyclarck Peon

    Messages:
    196
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks a lot to telling can you know the site or tell me more about Bubble sort where can i read it.
     
    honeyclarck, Apr 19, 2012 IP
  6. rok86

    rok86 Greenhorn

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #6
    Bubble sort is also a type of algorithm which is use to sort the given set of number .
    you could read about it more from wikipedia or search engine
     
    rok86, Apr 19, 2012 IP
  7. javamazon

    javamazon Active Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    66
    #7
    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.

    In Term of java this is simple program :

    public class bubbleSort{
    public static void main(String a[]){
    int i;
    int array[] = {12,9,4,99,120,1,3,10};
    System.out.println("Values Before the sort:\n");
    for(i = 0; i < array.length; i++)
    System.out.print( array+" ");
    System.out.println();
    bubble_srt(array, array.length);
    System.out.print("Values after the sort:\n");
    for(i = 0; i <array.length; i++)
    System.out.print(array+" ");
    System.out.println();
    System.out.println("PAUSE");
    }

    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;
    }
    }
    }
    }
     
    javamazon, Apr 20, 2012 IP