Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort Boruvkas algorithm Bubble Sort vs Quick Sort Common Operations on various Data Structures Detect and Remove Loop in a Linked List How to Start Learning DSA Print kth least significant bit number Why is Binary Heap Preferred over BST for Priority Queue Bin Packing Problem Binary Tree Inorder Traversal Burning binary tree Equal Sum What is a Threaded Binary Tree? What is a full Binary Tree? Bubble Sort vs Merge Sort B+ Tree Program in Q language Deletion Operation from A B Tree Deletion Operation of the binary search tree in C++ language Does Overloading Work with Inheritance Balanced Binary Tree Binary tree deletion Binary tree insertion Cocktail Sort Comb Sort FIFO approach Operations of B Tree in C++ Language Recaman’s Sequence Tim Sort Understanding Data Processing Applications of trees in data structures Binary Tree Implementation Using Arrays Convert a Binary Tree into a Binary Search Tree Create a binary search tree Horizontal and Vertical Scaling Invert binary tree LCA of binary tree Linked List Representation of Binary Tree Optimal binary search tree in DSA Serialize and Deserialize a Binary Tree Tree terminology in Data structures Vertical Order Traversal of Binary Tree What is a Height-Balanced Tree in Data Structure Convert binary tree to a doubly linked list Fundamental of Algorithms Introduction and Implementation of Bloom Filter Optimal binary search tree using dynamic programming Right side view of binary tree Symmetric binary tree Trim a binary search tree What is a Sparse Matrix in Data Structure What is a Tree in Terms of a Graph What is the Use of Segment Trees in Data Structure What Should We Learn First Trees or Graphs in Data Structures All About Minimum Cost Spanning Trees in Data Structure Convert Binary Tree into a Threaded Binary Tree Difference between Structured and Object-Oriented Analysis FLEX (Fast Lexical Analyzer Generator) Object-Oriented Analysis and Design Sum of Nodes in a Binary Tree What are the types of Trees in Data Structure What is a 2-3 Tree in Data Structure What is a Spanning Tree in Data Structure What is an AVL Tree in Data Structure Given a Binary Tree, Check if it's balanced

Bucket Sort

Bucket Sort: In the sorting algorithm, we create buckets and put elements into them. We can apply some sorting algorithm (insertion sort) to sort the elements in each bucket. Finally, we take the elements out and join them to get the sorted result.

The bucket sort assumes that the input is generated by a random process that distributes the elements uniformly over the interval (0,1).

The idea of ??bucket sort is to divide the interval 0 to 1 into “n” equal-sized sub-intervals or buckets, and then distribute the n input number to the bucket.

The bucket sort program requires an auxiliary array X [0, 1, 2, ...., n-1] of the linked list.

Complexity table of bucket sort

ComplexityBest caseAverage caseWorst case
TimeO(n + k)O(n + k)O(n2)
Space  O(n)

Algorithm of bucket sort

Bucket Sort() 
Step 1: Create N buckets and each buckets is hold a range of values. 
Step 2: for i ? 1 to n     // initialize each bucket with 0 values. 
Step 3: for i ? 1 to n     put elements into buckets matching the range. 
Step 4: for i ? 1 to n     sort elements in each bucket.     gather elements from each bucket 
Step 5: exit

For example: Suppose we have the following array, which we have to sort.

11861252

Step 1: Create a new array, and this array size is 10. Every field of this array is used as a bucket.  

Step 2: Insert the array element into the new array bucket, and these elements will be added according to the range of the bucket.    

Step 3: Gather the element form each bucket.  

Bucket sort program in C language:

#include <stdio.h> 
 void Bucket_Sort(int array[], int n) 
 {       
 int i, j;    
    int count[n];   
    for (i = 0; i < n; i++)     
      count[i] = 0;       
 for (i = 0; i < n; i++)   
       (count[array[i]])++;   
      for (i = 0, j = 0; i < n; i++)   
         for(; count[i] > 0; (count[i])--)  
            array[j++] = i;   
} 
      int main() 
 {    
  int array[100], i, num;  
        printf("Enter the size of array : ");   
      scanf("%d", &num);  
       printf("Enter the %d elements to be sorted:\n",num); 
      for (i = 0; i < num; i++)  
        scanf("%d", &array[i]);  
     printf("\nThe array of elements before sorting : \n");  
    for (i = 0; i < num; i++)  
        printf("%d ", array[i]);  
      printf("\nThe array of elements after sorting : \n");  
     Bucket_Sort(array, num);   
    for (i = 0; i < num; i++)  
        printf("%d ", array[i]);   
      printf("\n");        
   return 0; 
 } 

Bucket sort program in java language:

import java.util.ArrayList; 
import java.util.Collections;  
 public class BucketSort 
{  
 public void bucketSort(float[] arr, int n)
 {     
if (n <= 0)       return;  
   @SuppressWarnings("unchecked")   
  ArrayList<Float>[] bucket = new ArrayList[n];       // Create empty buckets   
  for (int i = 0; i < n; i++)   
    bucket[i] = new ArrayList<Float>();       // Add elements into the buckets     
for (int i = 0; i < n; i++)
 {     
  int bucketIndex = (int) arr[i] * n;  
     bucket[bucketIndex].add(arr[i]);  
   }  
     // Sort the elements of each bucket     for (int i = 0; i < n; i++) 
{    
   Collections.sort((bucket[i]));   
  }    
   // Get the sorted array     int index = 0;    
 for (int i = 0; i < n; i++) 
{   
    for (int j = 0, size = bucket[i].size(); j < size; j++) 
{      
   arr[index++] = bucket[i].get(j); 
      }    
 }  
 }  
   // Driver code   public static void main(String[] args) 
{   
  BucketSort b = new BucketSort();   
  float[] arr = { (float) 0.42, (float) 0.32, (float) 0.33, (float) 0.52, (float) 0.37, (float) 0.47,         (float) 0.51 };   
  b.bucketSort(arr, 7); 
      for (float i : arr)       System.out.print(i + "  "); 
  } 
}



ADVERTISEMENT
ADVERTISEMENT