PRESENTATION ON CSC4425
Presented by:
U/CS/16/415
U/CS/16/422
U/CS/16/417
Title
Bubble sort and radix sort
Introduction
• Bubble sort, sometimes referred to as sinking sort, is a
simple sorting algorithm that repeatedly steps through
the list, compares adjacent pairs and swaps them if they
are in the wrong order. The pass through the list is
repeated until the list is sorted. The algorithm, which is a
comparison sort, is named for the way smaller or larger
elements "bubble" to the top of the list. Although the
algorithm is simple, it is too slow and impractical for most
problems even when compared to insertion sort.
Introduction
In computer science, radix sort is a non-comparative integer sorting
algorithm that sorts data with integer keys by grouping keys by the individual
digits which share the same significant position and value. A positional
notation is required, but because integers can represent strings of characters
(e.g., names or dates) and specially formatted floating point numbers, radix
sort is not limited to integers
Design and implement a bubble sort
#include <iostream>
using namespace std;
/* function to sort arr using shellSort */
int shellSort(int arr[], int n)
{
int i, j, no_move=0, comp=0;
// Start with a big gap, then reduce the gap
for (int gap = n/2; gap > 0; gap /= 2)
{
// Do a gapped insertion sort for this gap size.
// The first gap elements a[0..gap-1] are already in gapped order
// keep adding one more element until the entire array is
// gap sorted
for (int i = gap; i < n; i += 1)
{
// add a[i] to the elements that have been gap sorted
// save a[i] in temp and make a hole at position i
int temp = arr[i];
// shift earlier gap-sorted elements up until the correct
// location for a[i] is found
int j;
Bubble code con’t….
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];
no_move++;
// put temp (the original a[i]) in its correct location
arr[j] = temp;
comp++;
cout << "nnumber of moves: nn"<<no_move;
cout << "nnumber of comparison: nn"<<comp;
}
}
return 0;
}
void printArray(int arr[], int n)
{
for (int i=0; i<n; i++)
cout << arr[i] << " ";
}
Bubble sort code
int main()
{
int arr[] = {47, 88, 78, 31, 81, 32, 54, 91, 9, 46,
96, 99, 74, 21, 77, 35, 93, 52, 7, 89,
47, 88, 78, 31, 81, 32, 54, 91, 9, 46,
96, 99, 74, 21, 77, 35, 93, 52, 7, 89,
37, 6, 73, 87, 67, 95, 86, 33, 10, 56,
27, 100, 36, 42, 57, 98, 15, 5, 28, 22,
84, 82, 14, 94, 25, 68, 43, 20, 44, 58,
47, 88, 78, 31, 81, 32, 54, 91, 9, 46,
96, 99, 74, 21, 77, 35, 93, 52, 7, 89,
37, 6, 73, 87, 67, 95, 86, 33, 10, 56, }, i;
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Array before sorting: n n";
printArray(arr, n);
shellSort(arr, n);
cout << "nnArray after sorting: nn";
printArray(arr, n);
return 0;
screenshot
Radix code
#include <iostream>
using namespace std;
// Get maximum value from array.
int getMax(int arr[], int n)
{
int max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
// Count sort of arr[].
void countSort(int arr[], int n, int exp)
{
int i, j, no_move=0, comp=0;
// Count[i] array will be counting the number of array values having that 'i' digit at their (exp)th place.
int output[n], count[100] = {0};
// Count the number of times each digit occurred at (exp)th place in every input.
for (i = 0; i < n; i++)
count[(arr[i] / exp) % 100]++;
int temp = arr[i];
// Calculating their cumulative count.
for (j = 1; j < 10; j++)
count[i] += count[i-1];
Radix code con’t…
• // Calling countSort() for digit at (exp)th place in every
input.
• for (exp = 1; m/exp > 0; exp *= 10)
• countSort(arr, n, exp);
• }
• int main()
• {
• int arr[] = {47, 88, 78, 31, 81, 32, 54, 91, 9, 46,
• 96, 99, 74, 21, 77, 35, 93, 52, 7, 89,
• 37, 6, 73, 87, 67, 95, 86, 33, 10, 56,
• 27, 100, 36, 42, 57, 98, 15, 5, 28, 22,
• 84, 82, 14, 94, 25, 68, 43, 20, 44, 58,
• 47, 88, 78, 31, 81, 32, 54, 91, 9, 46,
• 96, 99, 74, 21, 77, 35, 93, 52, 7, 89,
• 37, 6, 73, 87, 67, 95, 86, 33, 10, 56,
• 27, 100, 36, 42, 57, 98, 15, 5, 28, 22,
• 84, 82, 14, 94, 25, 68, 43, 20, 44, 58}, i;
•
• int n = sizeof(arr)/sizeof(arr[0]);
• cout << "Array before sorting: n n";
• getMax(arr, n);
• radixsort(arr, n);
• cout << "nnArray after sorting: nn";
• getMax(arr, n);
• return 0;
• }
screenshot
Number bubblesort radixsort radio(bu/ra)
Compare
100
500
1000
Move
100
500
1000
503
3506
8006
503
3506
8006
503
3506
8006
503
3506
8006
1
1
1
1
1
1
• Radix Sort
• The lower bound for Comparison based sorting algorithm
(Merge Sort, Heap Sort, Quick-Sort .. etc) is Ω(nLogn), i.e., they
cannot do better than nLogn.
• Counting sort is a linear time sorting algorithm that sort in
O(n+k) time when elements are in range from 1 to k.
• What if the elements are in range from 1 to n2?
We can’t use counting sort because counting sort will take O(n2)
which is worse than comparison based sorting algorithms. Can
we sort such an array in linear time?
Radix Sort is the answer. The idea of Radix Sort is to do digit by
digit sort starting from least significant digit to most significant
digit. Radix sort uses counting sort as a subroutine to sort.
• The Radix Sort Algorithm
1) Do following for each digit i where i varies from least
significant digit to the most significant digit.
………….a) Sort input array using counting sort (or any stable sort)
according to the i’th digit.
What is the running time of Radix Sort?
Let there be d digits in input integers. Radix Sort takes
O(d*(n+b)) time where b is the base for representing
numbers, for example, for decimal system, b is 10. What is
the value of d? If k is the maximum possible value, then d
would be O(logb(k)). So overall time complexity is O((n+b) *
logb(k)). Which looks more than the time complexity of
comparison based sorting algorithms for a large k. Let us first
limit k. Let k <= nc where c is a constant. In that case, the
complexity becomes O(nLogb(n)). But it still doesn’t beat
comparison based sorting algorithms.
What if we make value of b larger?. What should be the
value of b to make the time complexity linear? If we set b as
n, we get the time complexity as O(n).
• In other words, we can sort an array of integers with range from
1 to nc if the numbers are represented in base n (or every digit
takes log2(n) bits).
• Is Radix Sort preferable to Comparison based sorting
algorithms like Quick-Sort?
If we have log2n bits for every digit, the running time of Radix
appears to be better than Quick Sort for a wide range of input
numbers. The constant factors hidden in asymptotic notation
are higher for Radix Sort and Quick-Sort uses hardware caches
more effectively. Also, Radix sort uses counting sort as a
subroutine and counting sort takes extra space to sort numbers.
• Implementation of Radix Sort
Following is a simple implementation of Radix Sort. For
simplicity, the value of d is assumed to be 1000. We recommend
you to see Counting Sort for details of countSort() function in
below code.
Thanks
for
listening

Analysis of algorithms

  • 1.
    PRESENTATION ON CSC4425 Presentedby: U/CS/16/415 U/CS/16/422 U/CS/16/417 Title Bubble sort and radix sort
  • 2.
    Introduction • Bubble sort,sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list, compares adjacent pairs and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort.
  • 3.
    Introduction In computer science,radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value. A positional notation is required, but because integers can represent strings of characters (e.g., names or dates) and specially formatted floating point numbers, radix sort is not limited to integers
  • 4.
    Design and implementa bubble sort #include <iostream> using namespace std; /* function to sort arr using shellSort */ int shellSort(int arr[], int n) { int i, j, no_move=0, comp=0; // Start with a big gap, then reduce the gap for (int gap = n/2; gap > 0; gap /= 2) { // Do a gapped insertion sort for this gap size. // The first gap elements a[0..gap-1] are already in gapped order // keep adding one more element until the entire array is // gap sorted for (int i = gap; i < n; i += 1) { // add a[i] to the elements that have been gap sorted // save a[i] in temp and make a hole at position i int temp = arr[i]; // shift earlier gap-sorted elements up until the correct // location for a[i] is found int j;
  • 5.
    Bubble code con’t…. for(j = i; j >= gap && arr[j - gap] > temp; j -= gap) arr[j] = arr[j - gap]; no_move++; // put temp (the original a[i]) in its correct location arr[j] = temp; comp++; cout << "nnumber of moves: nn"<<no_move; cout << "nnumber of comparison: nn"<<comp; } } return 0; } void printArray(int arr[], int n) { for (int i=0; i<n; i++) cout << arr[i] << " "; }
  • 6.
    Bubble sort code intmain() { int arr[] = {47, 88, 78, 31, 81, 32, 54, 91, 9, 46, 96, 99, 74, 21, 77, 35, 93, 52, 7, 89, 47, 88, 78, 31, 81, 32, 54, 91, 9, 46, 96, 99, 74, 21, 77, 35, 93, 52, 7, 89, 37, 6, 73, 87, 67, 95, 86, 33, 10, 56, 27, 100, 36, 42, 57, 98, 15, 5, 28, 22, 84, 82, 14, 94, 25, 68, 43, 20, 44, 58, 47, 88, 78, 31, 81, 32, 54, 91, 9, 46, 96, 99, 74, 21, 77, 35, 93, 52, 7, 89, 37, 6, 73, 87, 67, 95, 86, 33, 10, 56, }, i; int n = sizeof(arr)/sizeof(arr[0]); cout << "Array before sorting: n n"; printArray(arr, n); shellSort(arr, n); cout << "nnArray after sorting: nn"; printArray(arr, n); return 0;
  • 7.
  • 8.
    Radix code #include <iostream> usingnamespace std; // Get maximum value from array. int getMax(int arr[], int n) { int max = arr[0]; for (int i = 1; i < n; i++) if (arr[i] > max) max = arr[i]; return max; } // Count sort of arr[]. void countSort(int arr[], int n, int exp) { int i, j, no_move=0, comp=0; // Count[i] array will be counting the number of array values having that 'i' digit at their (exp)th place. int output[n], count[100] = {0}; // Count the number of times each digit occurred at (exp)th place in every input. for (i = 0; i < n; i++) count[(arr[i] / exp) % 100]++; int temp = arr[i]; // Calculating their cumulative count. for (j = 1; j < 10; j++) count[i] += count[i-1];
  • 9.
    Radix code con’t… •// Calling countSort() for digit at (exp)th place in every input. • for (exp = 1; m/exp > 0; exp *= 10) • countSort(arr, n, exp); • } • int main() • { • int arr[] = {47, 88, 78, 31, 81, 32, 54, 91, 9, 46, • 96, 99, 74, 21, 77, 35, 93, 52, 7, 89, • 37, 6, 73, 87, 67, 95, 86, 33, 10, 56, • 27, 100, 36, 42, 57, 98, 15, 5, 28, 22, • 84, 82, 14, 94, 25, 68, 43, 20, 44, 58, • 47, 88, 78, 31, 81, 32, 54, 91, 9, 46, • 96, 99, 74, 21, 77, 35, 93, 52, 7, 89, • 37, 6, 73, 87, 67, 95, 86, 33, 10, 56, • 27, 100, 36, 42, 57, 98, 15, 5, 28, 22, • 84, 82, 14, 94, 25, 68, 43, 20, 44, 58}, i; • • int n = sizeof(arr)/sizeof(arr[0]); • cout << "Array before sorting: n n"; • getMax(arr, n); • radixsort(arr, n); • cout << "nnArray after sorting: nn"; • getMax(arr, n); • return 0; • }
  • 10.
  • 11.
    Number bubblesort radixsortradio(bu/ra) Compare 100 500 1000 Move 100 500 1000 503 3506 8006 503 3506 8006 503 3506 8006 503 3506 8006 1 1 1 1 1 1
  • 12.
    • Radix Sort •The lower bound for Comparison based sorting algorithm (Merge Sort, Heap Sort, Quick-Sort .. etc) is Ω(nLogn), i.e., they cannot do better than nLogn. • Counting sort is a linear time sorting algorithm that sort in O(n+k) time when elements are in range from 1 to k. • What if the elements are in range from 1 to n2? We can’t use counting sort because counting sort will take O(n2) which is worse than comparison based sorting algorithms. Can we sort such an array in linear time? Radix Sort is the answer. The idea of Radix Sort is to do digit by digit sort starting from least significant digit to most significant digit. Radix sort uses counting sort as a subroutine to sort. • The Radix Sort Algorithm 1) Do following for each digit i where i varies from least significant digit to the most significant digit. ………….a) Sort input array using counting sort (or any stable sort) according to the i’th digit.
  • 13.
    What is therunning time of Radix Sort? Let there be d digits in input integers. Radix Sort takes O(d*(n+b)) time where b is the base for representing numbers, for example, for decimal system, b is 10. What is the value of d? If k is the maximum possible value, then d would be O(logb(k)). So overall time complexity is O((n+b) * logb(k)). Which looks more than the time complexity of comparison based sorting algorithms for a large k. Let us first limit k. Let k <= nc where c is a constant. In that case, the complexity becomes O(nLogb(n)). But it still doesn’t beat comparison based sorting algorithms. What if we make value of b larger?. What should be the value of b to make the time complexity linear? If we set b as n, we get the time complexity as O(n).
  • 14.
    • In otherwords, we can sort an array of integers with range from 1 to nc if the numbers are represented in base n (or every digit takes log2(n) bits). • Is Radix Sort preferable to Comparison based sorting algorithms like Quick-Sort? If we have log2n bits for every digit, the running time of Radix appears to be better than Quick Sort for a wide range of input numbers. The constant factors hidden in asymptotic notation are higher for Radix Sort and Quick-Sort uses hardware caches more effectively. Also, Radix sort uses counting sort as a subroutine and counting sort takes extra space to sort numbers. • Implementation of Radix Sort Following is a simple implementation of Radix Sort. For simplicity, the value of d is assumed to be 1000. We recommend you to see Counting Sort for details of countSort() function in below code.
  • 15.