Bubblesort

[Solved] Bubblesort | C - Code Explorer | yomemimo.com
Question : bubblesort

Answered by : keen-kingfisher

"""Bubblesort
"""
## Un-optimised--------------------------------------------------------------
def bubble_1(lst): n = len(lst) - 1 for i in range(n): # Within the unsorted portion for j in range(n - i): # If curr > next, swap if lst[j] > lst[j+1]: lst[j], lst[j+1] = lst[j+1], lst[j] return lst # for easy testing
def bubble_2(lst): n = len(lst) - 1 # Within the unsorted portion, except the last number for unsorted in range(n, 0, -1): for i in range(unsorted): # If curr > next, swap if lst[i] > lst[i+1]: lst[i], lst[i+1] = lst[i+1], lst[i] return lst # for easy testing
## Optimised-----------------------------------------------------------------
def bubble_3(lst): n = len(lst) - 1 # Within the unsorted portion, except the last number for unsorted in range(n, 0, -1): swapped = False for i in range(unsorted): # If curr > next, swap if lst[i] > lst[i+1]: lst[i], lst[i+1] = lst[i+1], lst[i] swapped = True # Check if its sorted by this time if not swapped: break return lst # for easy testing

Source : https://www.programiz.com/dsa/bubble-sort | Last Update : Fri, 19 Aug 22

Question : bubblesort

Answered by : m-haris-imran

Void bubbleSort(int arr[ ], int n){ For( int I = 0; I < n-1; I++){ //outer loop for iterating to n-1 elements For( int j = 0; j < n-I-1; j++){ //inner loop for checking each element If (arr[ j ] > arr[ j+1 ]{ // For swapping if previous element is greater than next one Swap( arr[ j ], arr[ j+1 ]); } } }
}

Source : https://www.codingninjas.com/blog/2020/07/16/sorting-in-data-structure-categories-types/ | Last Update : Thu, 01 Sep 22

Answers related to bubblesort

Code Explorer Popular Question For C