How recursive bubble sort work

                                              Recursive Bubble Sort Base Case: If array size is 1, return. Do One Pass of normal Bubble Sort. This pass fixes last element of current subarray. Recur for all elements except last of current subarray. Example: Index=     0 1  2 3 4 5 6 7   100                          90 80 70 60 50 40 30  Array []= Suppose we want to sort the above in the increasing order by the recursive bubble sort. Make a get grater which will return the grater of the array. Compare the first and last element of the array if the first is greater than last the swap with each other. And again call the sort function and decrement the array size by 1; 1) 30                     90 80 70 60 50 40 100    ...

How comb sort work

 

                                                     Comb sort

    The comb sort has the following step:

1)      Create and anilities the variable gap  swap and constant

Shrink factor

a)      Gap=size of array

b)      Swap=false

c)      Shrink factor =1.3

2)      Set swap =false

3)      Set gap=gap/shrink factor

4)      Iterate over the array from i=0

To n<n-gap 

If array[i]>array[i+gap]

a)      Swap the element array[i] and array[i+gap]

b)      Set swap=true

5)      Repeat the step 2-4 while gap1=1 and swap=true

 

Let us solve the example

Array{7,5,1,50,3,-20,25,-4,30,0}

Gap=10

K=1.3

1st pass

After update gap value =10/1.3=7

7,5,1,50,3, -20,25, -4,30,0

-4,5,1,50,3,-20,25,7,30,0

-4,5,0,50,3,-20,25,7,30,1

2nd pass

After update gap value =7/1.3=5

-4,5,50,3,-20,25,7,30,1

-20,5,0,50,3,-4,25,7,30,1

-20,5,0,30,3,-4,25,7,50,1

-20,5,0,30,1,-4,25,7,50,3

 

3rd pass

After update gap value 5/1.3=3

-20,5,0,50,3,-4,25,7,30,1

-20,3,0,50,5,-4,25,7,30,1

-20,3,-4,30,5,0,25,7,50,1

-20,3,-4,25,5,0,30,7,50,1

20,3,-4,25,5,0,1,7,50,30

4th pass

After update gap value =3/1.3=2

-20,3, -4,25,5,,0,30,7,50,1          

-20,3, -4,0,5,25,1,7,50,30,

-20,3,-4,0,1,25,5,7,50,30

-20,3,-4,0,1,7,5,25,50,30

5th  pass

 

After update the gap value 2/1.3=1

-20,3,-4,0,1,7,5,25,50,30,

-20,-4,3,0,1,7,5,25,50,30,

-20,-4,0,3,1,7,5,25,50,30

-20,-4,0,3,1,7,5,25,50,30

-20,-4,0,1,3,7,5,25,50,30

-20,4,0,1,3,5,7,25,50,30

 

 

Comments

Popular posts from this blog

simple calculator apps project in android studio

Arrays in C Programming Language

Pointers in C