Bubble sort in C to arrange numbers in ascending order; you can modify it for descending order and can also sort strings. The bubble sort algorithm isn't efficient as its both average-case as well as worst-case complexity are O(n2).
The steps of the Bubble sort algorithm are:
STEP 1: Start at index zero, compare the element with the next one (a[0] & a[1] (a is the name of the array)), and swap if a[0] > a[1]. Now compare a[1] & a[2] and swap if a[1] > a[2]. Repeat this process until the end of the array. After doing this, the largest element is present at the end. This whole thing is known as a pass. In the first pass, we process array elements from [0,n-1].
STEP 2: Repeat step one but process array elements [0, n-2] because the last one, i.e., a[n-1], is present at its correct position. After this step, the largest two elements are present at the end.
Bubble sort in C to arrange numbers in ascending order; you can modify it for descending order and can also sort strings. The bubble sort algorithm isn't efficient as its both average-case as well as worst-case complexity are O(n2).
The steps of the Bubble sort algorithm are:
STEP 1: Start at index zero, compare the element with the next one (a[0] & a[1] (a is the name of the array)), and swap if a[0] > a[1]. Now compare a[1] & a[2] and swap if a[1] > a[2]. Repeat this process until the end of the array. After doing this, the largest element is present at the end. This whole thing is known as a pass. In the first pass, we process array elements from [0,n-1].
STEP 2: Repeat step one but process array elements [0, n-2] because the last one, i.e., a[n-1], is present at its correct position. After this step, the largest two elements are present at the end.
STEP 3: Repeat this process n-1 times.
5