×

Sorting Algorithms

Sorting: In the data structure, sorting is the process by which you arrange the data in a logical order. This logical order can also be an ascending order or a descending order.

Sorting is related to searching. We search for many things in our life, such as a topic on google, a page in the book, a word in the dictionary, and roll number in the exam hall. Therefore, all these things that happen are sorted so that we can easily search them.

Types of sorting

There are two types of sorting in the data structure:

  1. Internal sorting
  2. External sorting

Internal sorting: The data to be sorted in this sorting resides in the main memory. There are the following types of internal sorting.

  1. Bubble sort
  2. Insertion sort
  3. Quick sort
  4. Heap sort
  5. Selection sort

External sorting: The data to be sorted in this sorting resides in secondary memory. There is so much data in this sorting that it cannot enter the main memory. There is only one type of external sorting called merge sort.

Bubble sort

Bubble sort is a very easy sorting technique. It compares all the elements one by one. It sorts them based on their values. In this, the two elements of the beginning are compared. If the first element is larger than the second element, it will change both elements' location, and this comparison will go on till the end.

Complexity table of bubble sort

ComplexityBest caseAverage caseWorst case
TimeO(n)O(n2)O(n2)
Space  O(1)

Algorithm of bubble sort

The following algorithm is used to implement a bubble sort.

Bubble_Sort(A)
Step 1: Repeat Step 2 and 3 for i = 1, 2….to n-1
Step 2: set j = 1
Step 3: Repeat while (j < n - i)              
{                
if (A[j] > A[j + 1])                 swap A[j] and A[j + 1]                 j = j + 1              
}
// end of while loop                  
// end of for loop    
Step 4: exit

For example: Let’s consider an array A{8. 14, 6, 7, 32, 15}.

  8 14 6 7 32 15  

First iteration
Step 1: Compare the first two elements, 8 < 14.             
No interchange
8 14 6 7 32 15  
Step 2: Compare the second and third elements, 14 > 6.            
The second element is larger than the third element, so it will swap the elements.       
8 6 14 7 32 15  
Step 3: Compare the third and fourth elements, 14 > 7.            
The third element is larger than the fourth element, so it will swap the elements.     
8 6 7 14 32 15  
Step 4: Compare the fourth and fifth elements, 14 < 32.             
No interchange  
8 6 14 7 32 15  
Step 5: Compare the fifth and sixth elements, 32 > 15.                                     
The fifth element is larger than the sixth element, so it will swap the elements.  
8 6 14 7 15 32                     
The first iteration is completed. Similarly, all iterations will be performed.  

Bubble sort program in C language.

#include<stdio.h>
 void main ()
   {      
 int i, j,temp;       
 int a[10] = {8, 7, 9, 1, 3, 2, 5, 10, 6, 4};        
for(i = 0; i<10; i++)       
{         
  for(j = i+1; j<10; j++) 
          {              
 if(a[j] > a[i])               
{                  
temp = a[i];                  
a[i] = a[j];                  
a[j] = temp;                 
}          
   }        
}   
printf("Bubble sort list\n"); 
  for(i = 0; i<10; i++) 
  {   printf("%d\n",a[i]);   }
        }    

Output:

Bubble sort list
1
2
3
4
5
6
7
8
9
10

Bubble sort program in java language

class BubbleSort
{            
void bubbleSort(int arr[])            
{                       
  int n = arr.length;                        
for (int i = 0; i < n-1; i++)                                   
  for (int j = 0; j < n-i-1; j++)                                                
if (arr[j] > arr[j+1])                                                
{          // swap arr[j+1] and arr[i]                  
  int temp = arr[j];                                                            
arr[j] = arr[j+1];                                                            
arr[j+1] = temp;                                                
}            
}               /* Prints the array */            
void printArray(int arr[])            
{                         int n = arr.length;                        
for (int i=0; i<n; ++i)                                   
System.out.print(arr[i] + " ");                      
   System.out.println();            
}               // Driver method to test above            
public static void main(String args[])            
{                        
BubbleSort ob = new BubbleSort();                        
int arr[] = {8, 7, 9, 1, 3, 2, 5, 10, 6, 4};                       
  ob.bubbleSort(arr);                  
       System.out.println("Bubble sort list");                       
  ob.printArray(arr);             } }  

Output:

Bubble sort list
1
2
3
4
5
6
7
8
9
10

Related Topics

Sum of Nodes in a Binary Tree

In this article, we will see the sample problems that will help us understand the concept and summation of all the nodes in the binary tree. Implementation /* creating a program that...

4 minutes read.

Optimal binary search tree using dynamic programming

Implementation // We are creating a presentation where we will present a recursive method of the optimal binary search tree problem.  #include <bits/stdc++.h> using namespace std; //creating a utility function that will help us...

9 minutes read.

Hashing and its Applications

Hashing Hashing refers to transforming plain text data in such a way that even if it is leaked for some reason, no one would be able to make sense of it....

6 minutes read.

B Tree in Data Structure

Data management is called database management. A data model is a system that stores, manages, and optimizes computer resources. Data processing is not just about data storage. Almost every app...

9 minutes read.

Operations on 2D-Arrays

Two Dimensional Array Operations Adding Elements to Two-D Arrays We must put data in both rows and columns when inserting items in 2-D Arrays. As a result, we employ the idea of...

10 minutes read.

Fundamental of Algorithms

An algorithm is a part of any programming solution or coding. If we have to make a solution then first we have to think of a clear idea about the...

13 minutes read.

Time Complexity of Selection Sort in Data Structure

What is Time Complexity? The term “Time complexity” can be defined as the number of times executions made of a particular sequence of instructions and not the total amount of time...

3 minutes read.

Operations on Queue in Data Structures

A queue is a linear structure where operations are done in a specific sequence. Queues are abstract data structures that are comparable to Stacks. A queue, unlike a stack, is...

8 minutes read.

Linear vs Binary Search: Data Structure

Difference Between Linear and Binary Search What is Linear Search? A linear search also referred as a sequential search. It is a way to find an element within a list and it...

3 minutes read.

Hash Table vs STL Map

Hash table and STL map are extremely valuable information structures in software engineering. Here we will consider the examination between their properties to be well as execution.  To start with, we will...

7 minutes read.

Deque in Data Structure

Deque A deque referred as “Double-Ended Queue”, is a linear collection of data items same like queue data structure. deque has two ends, front end and rear end, deque is the...

27 minutes read.

Tree in Data Structure

Tree A tree is a non-linear data structure by which hierarchical data is displayed. As we know that there are many trees in the forest, similarly the data structure also contains...

3 minutes read.

Common Operations on various Data Structures

Data structures are ways to organise data in computer memory for quick and effective use. The storage of data uses a variety of data-structures. It is also possible to define...

7 minutes read.

Linear vs Non-Linear: Data Structure

What is Linear Data Structure? The data structure is said to be linear if the data elements are arranged linearly or we can say sequentially. In the linear data structure, the...

3 minutes read.

Understanding Data Processing

Introduction Data In our everyday lives, any task that we perform online is related to data. Millions of pieces of data are produced every second across the globe. Data production is largely...

4 minutes read.

Print kth least significant bit number

You have given a number and you have to find out the kth least significant bit of this number. K will be given to you.  The bit will be from...

3 minutes read.

Length of longest palindrome in a linked list using O(1) extra space

Length of longest palindrome in a linked list using O(1) extra space In this problem, we need to find the length of the longest palindrome list that is present in given...

2 minutes read.

Operations of B Tree in C++ Language

B tree tends to be a self-aligning and balancing tree that helps us organise our data and document safely. We know that every data or information in the B tree...

9 minutes read.

What is a Height-Balanced Tree in Data Structure

A height-balanced tree is a type of binary tree. If the absolute difference between the heights of the left and right subtree is less than or equal to 1, then...

6 minutes read.

Equal Sum

Find an element in array such that the sum of left array is equal to the sum of right array You have been given an array of numbers. You have to...

4 minutes read.