Bubble Sort Code

[Solved] Bubble Sort Code | C - Code Explorer | yomemimo.com
Question : bubble sort

Answered by : prabhu-kiran-konda

def bubble_sort(nums): n = len(nums) for i in range(n): swapped = False for j in range(1, n - i): if nums[j] < nums[j - 1]: nums[j], nums[j - 1] = nums[j - 1], nums[j] swapped = True if not swapped: break return nums
print(bubble_sort([9, 8, 7, 6, 5, 4, 3, 2, 1]))

Source : | Last Update : Sat, 24 Sep 22

Question : bubble sort

Answered by : healthy-hare-aoylwn8hvx1u

// C program for implementation of Bubble sort
#include <stdio.h>
  
void swap(int *xp, int *yp)
{
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}
  
// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
   int i, j;
   for (i = 0; i < n-1; i++)      
  
       // Last i elements are already in place   
       for (j = 0; j < n-i-1; j++) 
           if (arr[j] > arr[j+1])
              swap(&arr[j], &arr[j+1]);
}
  
/* Function to print an array */
void printArray(int arr[], int size)
{
    int i;
    for (i=0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
}
  
// Driver program to test above functions
int main()
{
    int arr[] = {64, 34, 25, 12, 22, 11, 90};
    int n = sizeof(arr)/sizeof(arr[0]);
    bubbleSort(arr, n);
    printf("Sorted array: \n");
    printArray(arr, n);
    return 0;
}

Source : https://www.geeksforgeeks.org/bubble-sort/ | Last Update : Fri, 25 Feb 22

Question : bubble sort

Answered by : vamsha-tamu

arr = [12,1,6,23,234,456,2,35,687,34]
# arry consists of 9 elements
n = len(arr)
#conts the element of the arry
for j in range(n-1): #this is for list to get shorted properly && you dont need to go full as it may take more steps to exicute for i in range(n-j-1): #if left with n the there will be array error because it will try to add 1 to n in below leading to array being bigger if arr[i]>arr[i+1]: arr[i],arr[i+1]=arr[i+1],arr[i] else: pass
# arry starts from last and eventually decrease the number lower and lower which leads to lesser steps
# #above took 125 steps to eully exicute
#################################################################
# #this takes 217 steps to run and end code
# for i in range(n):
# if arr[i]>arr[i+1]:
# arr[i],arr[i+1]=arr[i+1],arr[i]
# else:
# pass
#################################################################
print("the sorted array is : "arr)

Source : | Last Update : Wed, 13 Jul 22

Question : Bubble sort

Answered by : ill-ibis-otntsoyudu5w

public class BubbleSortExample { static void bubbleSort(int[] arr) { int n = arr.length; int temp = 0; for(int i=0; i < n; i++){ for(int j=1; j < (n-i); j++){ if(arr[j-1] > arr[j]){ //swap elements temp = arr[j-1]; arr[j-1] = arr[j]; arr[j] = temp; } } } } public static void main(String[] args) { int arr[] ={3,60,35,2,45,320,5}; System.out.println("Array Before Bubble Sort"); for(int i=0; i < arr.length; i++){ System.out.print(arr[i] + " "); } System.out.println(); bubbleSort(arr);//sorting array elements using bubble sort System.out.println("Array After Bubble Sort"); for(int i=0; i < arr.length; i++){ System.out.print(arr[i] + " "); } }
} 

Source : | Last Update : Sat, 12 Mar 22

Question : what is bubble sort algorithm

Answered by : md-asaduzzaman-atik

Bubble sort, aka sinking sort is a basic algorithm
for arranging a string of numbers or other elements
in the correct order. This sorting algorithm is
comparison-based algorithm in which each pair of
adjacent elements is compared and the elements are
swapped if they are not in order. The algorithm then
repeats this process until it can run through the
entire string or other elements and find no two
elements that need to be swapped. This algorithm is
not suitable for large data sets as its average and
worst case complexity are of Ο(n2) where n is the number of items.
In general, Just like the movement of air bubbles
in the water that rise up to the surface, each element
of the array move to the end in each iteration.
Therefore, it is called a bubble sort.

Source : https://github.com/teamroyalcoder/algorithms#bubble-sort-algorithm | Last Update : Fri, 02 Sep 22

Question : bubble sort

Answered by : sushant-mishra

import java.util.Arrays;
public class Bubble{	public static void main(String[] args){	int[] arr = {5,4,3,2,1}; // Test case array	for(int i =0;i<arr.length-1;i++){ // Outer Loop	boolean swap = false; for(int j =1;j<arr.length;j++){ // Inner Loop if(arr[j-1]>arr[j]){ // Swapping int temp = arr[j-1]; arr[j-1] = arr[j]; arr[j] = temp; swap = true; } } if(!swap){ // If you went through the whole arrray ones and break; // the elements did not swap it means the array is sorted hence stop }	}	System.out.print(Arrays.toString(arr));	}
}

Source : | Last Update : Sun, 01 May 22

Question : bubble sort code

Answered by : talented-tern-o3d7d79nul54

func Sort(arr []int) []int {	for i := 0; i < len(arr)-1; i++ {	for j := 0; j < len(arr)-i-1; j++ {	if arr[j] > arr[j+1] {	temp := arr[j]	arr[j] = arr[j+1]	arr[j+1] = temp	}	}	}	return arr
}

Source : | Last Update : Sun, 23 Aug 20

Question : bubble sort

Answered by : gocrazygh

// Go impelementation of Bubble Sort
package main
import "fmt"
func bubbleSort(arr []int) {	// Finding the length of the array	n := len(arr)	for i := 0; i < n; i++ {	for j := 0; j < n-i-1; j++ {	// if the fist lement is greater than the second one then swap	if arr[j] > arr[j+1] {	arr[j], arr[j+1] = arr[j+1], arr[j]	}	}	}
}
func printArray(arr []int) {	n := len(arr)	for i := 0; i < n; i++ {	fmt.Print(arr[i], " ")	}	fmt.Println()
}
func main() {	arr := []int{24,35,8,16,64}	bubbleSort(arr)	printArray(arr)
}

Source : | Last Update : Tue, 23 Aug 22

Question : bubble sort

Answered by : jitu-biswas

#include<iostream>
void swap(int &a, int &b)
{ int temp=a; a=b; b=temp;
}
void bubblesort(int arr[], int n)
{ for(int i=0;i<n-1;i++)// why upto n-1? { for(int j=0;j<n-1;j++) { if(arr[j]>arr[j+1]) { swap(arr[j],arr[j+1]); } } }
}
int main()
{ int arr[5]={7, 8, 1, -4, 0}; bubblesort(arr, 5); for(int i=0;i<5;i++) { std::cout<<arr[i]<<std::endl; }
}

Source : | Last Update : Wed, 11 May 22

Question : bubble sort

Answered by : md-asaduzzaman-atik

// Bubble Sort algorithm -> jump to line 21
...
#include <iostream> // Necessary for input output functionality
#include <bits/stdc++.h> // To simplify swapping process
...
...
/**
* Sort array of integers with Bubble Sort Algorithm
*
* @param arr Array, which we should sort using this function
* @param arrSZ The size of the array
* @param order In which order array should be sort
*
* @return Sorted array of integers
*/
void bubbleSortInt(double arr[], int arrSz, string order = "ascending")
{ for (int i = 0; i < arrSz; ++i) { for (int j = 0; j < (arrSz - i - 1); ++j) { // Swapping process if ((order == "descending") ? arr[j] < arr[j + 1] : arr[j] > arr[j + 1]) { swap(arr[j], arr[j + 1]); } } } return; // Optional because it's a void function
} // end bubbleSortInt
...
int main()
{
... return 0; // The program executed successfully.
} // end main

Source : https://github.com/teamroyalcoder/algorithms#bubble-sort-algorithm | Last Update : Fri, 02 Sep 22

Answers related to bubble sort code

Code Explorer Popular Question For C