Sorting an array in Bash using Bubble sort Last Updated : 25 Nov, 2021 Comments Improve Suggest changes Like Article Like Report Prerequisite: Bubble Sort Given an array arr sort the array in ascending order using bash scripting. Examples: Input : 9 7 2 5 Output : Array in sorted order : 2 5 7 9 Approach : For sorting the array bubble sort is the simplest technique. Bubble sort works by swapping the adjacent elements if they are in the wrong order. Example: Given array - (9, 7, 2, 5) After first iteration - (7, 2, 5, 9) After second iteration - (2, 5, 7, 9) and so on... In this way, the array is sorted by placing the greater element at the end of the array. # Sorting the array in Bash # using Bubble sort # Static input of Array arr=(10 8 20 100 12) echo "Array in original order" echo ${arr[*]} # Performing Bubble sort for ((i = 0; i<5; i++)) do for((j = 0; j<5-i-1; j++)) do if [ ${arr[j]} -gt ${arr[$((j+1))]} ] then # swap temp=${arr[j]} arr[$j]=${arr[$((j+1))]} arr[$((j+1))]=$temp fi done done echo "Array in sorted order :" echo ${arr[*]} Output : Array in sorted order : 8 10 12 20 100 Optimized Implementation: The above function always runs O(n^2) time even if the array is sorted. It can be optimized by stopping the algorithm if the inner loop didn’t cause any swap. n=5 arr=(10 8 20 100 12) echo "Original array is: ${arr[*]}"; flag=1; for (( i = 0; i < $n-1; i++ )) do flag=0; for ((j = 0; j < $n-1-$i; j++ )) do if [[ ${arr[$j]} -gt ${arr[$j+1]} ]] then temp=${arr[$j]}; arr[$j]=${arr[$j+1]}; arr[$j+1]=$temp; flag=1; fi done if [[ $flag -eq 0 ]]; then break; fi Output: Original array is: 10 8 20 100 12 Final sorted Array is 8 10 12 20 100 Worst and Average Case Time Complexity: O(n*n). The worst-case occurs when an array is reverse sorted. Best Case Time Complexity: O(n). The best-case occurs when the array is already sorted. Auxiliary Space: O(1) Boundary Cases: Bubble sort takes minimum time (Order of n) when elements are already sorted. Sorting In Place: Yes Stable: Yes Comment More infoAdvertise with us Next Article Sorting Algorithms Visualization : Bubble Sort M Manish_100 Follow Improve Article Tags : Technical Scripter Linux-Unix Shell Script Similar Reads Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Recursive Bubble Sort Background : Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.Example: First Pass: ( 5 1 4 2 8 ) --> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. ( 1 5 4 2 8 ) --> ( 1 4 10 min read Time and Space Complexity Analysis of Bubble Sort The time complexity of Bubble Sort is O(n^2) in the worst-case scenario and the space complexity of Bubble sort is O(1). Bubble Sort only needs a constant amount of additional space during the sorting process. Complexity TypeComplexityTime ComplexityBest: O(n)Average: O(n^2)Worst: O(n^2)Space Comple 3 min read Bubble Sort in different languagessin() in CThe sin() function in C is a standard library function to find the sine value of the radian angle. It is used to evaluate the trigonometric sine function of an angle. It takes the radian angle as input and returns the sin of that angle. It is defined inside <math.h> header file. Syntax of sin( 1 min read Bubble Sort in C++Bubble Sort Algorithm is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. It is often used to introduce the concept of a sorting and is particularly suitable for sorting small datasets.In this article, we will learn how to impleme 4 min read C++ Program for Recursive Bubble SortBackground: Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. Following is the iterative Bubble sort algorithm : // Iterative Bubble Sort bubbleSort(arr[], n) { for (i = 0; i n-1; i++) // Last i elements are already 2 min read Bubble Sort - PythonBubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. Bubble Sort algorithm, sorts an array by repeatedly comparing adjacent elements and swapping them if they are in the wrong order. The algorithm iterates through the a 2 min read Bubble Sort algorithm using JavaScriptBubble sort algorithm is an algorithm that sorts an array by comparing two adjacent elements and swapping them if they are not in the intended order. Here order can be anything like increasing or decreasing. How Bubble-sort works?We have an unsorted array arr = [ 1, 4, 2, 5, -2, 3 ], and the task is 4 min read p5.js | Bubble SortBubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. In order to know more about it. Please refer Bubble Sort Approach: Create an array of random values and render a bar corresponding to that value in terms of height. C 2 min read Sorting an array in Bash using Bubble sortPrerequisite: Bubble Sort Given an array arr sort the array in ascending order using bash scripting. Examples: Input : 9 7 2 5 Output : Array in sorted order : 2 5 7 9 Approach : For sorting the array bubble sort is the simplest technique. Bubble sort works by swapping the adjacent elements if they 2 min read Visualization of Bubble SortSorting Algorithms Visualization : Bubble SortThe human brain can easily process visuals in spite of long codes to understand the algorithms. In this article, Bubble sort visualization has been implemented using graphics.h library. As we all know that bubble sort swaps the adjacent elements if they are unsorted and finally the larger one being 5 min read Visualizing Bubble sort using PythonPrerequisites: Introduction to Matplotlib, Introduction to PyQt5, Bubble Sort Learning any algorithm can be difficult, and since you are here at GeekforGeeks, you definitely love to understand and implement various algorithms. It is tough for every one of us to understand algorithms at the first go. 2 min read Bubble Sort Visualization using JavaScriptGUI(Graphical User Interface) helps in better understanding than programs. In this article, we will visualize Bubble Sort using JavaScript. We will see how the elements are swapped in Bubble Sort and how we get the final sorted array. We will also visualize the time complexity of Bubble Sort. Refer 4 min read Bubble sort visualizer using PyGameIn this article we will see how we can visualize the bubble sort algorithm using PyGame i.e when the pygame application get started we can see the unsorted bars with different heights and when we click space bar key it started getting arranging in bubble sort manner i.e after every iteration maximum 3 min read Visualizing Bubble Sort using Tkinter in PythonIn this article, we will use the Python GUI Library Tkinter to visualize the Bubble Sort algorithm. Tkinter is a very easy to use and beginner-friendly GUI library that can be used to visualize the sorting algorithms.Here Bubble Sort Algorithm is visualized which works by repeatedly swapping the ad 5 min read Bubble Sort for Linked List by Swapping nodes Given a singly linked list, sort it using bubble sort by swapping nodes. Examples:Input: 5 -> 1 -> 32 -> 10 -> 78Output: 1 -> 5 -> 10 -> 32 -> 78 Input: 20 -> 4 -> 3Output: 3 -> 4 -> 20Approach: To apply Bubble Sort to a linked list, we need to traverse the list m 10 min read Sorting Strings using Bubble Sort Given an array of strings arr[]. Sort given strings using Bubble Sort and display the sorted array. In Bubble Sort, the two successive strings arr[i] and arr[i+1] are exchanged whenever arr[i]> arr[i+1]. The larger values sink to the bottom and are hence called sinking sort. At the end of each pa 4 min read Sort an array using Bubble Sort without using loops Given an array arr[] consisting of N integers, the task is to sort the given array by using Bubble Sort without using loops. Examples: Input: arr[] = {1, 3, 4, 2, 5}Output: 1 2 3 4 5 Input: arr[] = {1, 3, 4, 2}Output: 1 2 3 4 Approach: The idea to implement Bubble Sort without using loops is based o 9 min read Bubble Sort On Doubly Linked List Given a doubly linked list, the task is to sort the linked list in non-decreasing order by using bubble sort.Examples: Input : head: 5<->3<->4<->1<->2Output : head: 1<->2<->3<->4<->5Input : head: 5<->4<->3<->2Output : head: 2<-> 15+ min read Bubble sort using two Stacks Prerequisite : Bubble Sort Write a function that sort an array of integers using stacks and also uses bubble sort paradigm. Algorithm: 1. Push all elements of array in 1st stack 2. Run a loop for 'n' times(n is size of array) having the following : 2.a. Keep on pushing elements in the 2nd stack till 6 min read Like