Bubble Sort
#Bubble Sort
nums = [9, 4, 5, 1, 3, 7, 6]
for i in range(len(nums)):
for j in range(1, len(nums)):
if nums[j] < nums[j - 1]:
nums[j], nums[j - 1] = nums[j - 1], nums[j]
Bubble Sort
#Bubble Sort
nums = [9, 4, 5, 1, 3, 7, 6]
for i in range(len(nums)):
for j in range(1, len(nums)):
if nums[j] < nums[j - 1]:
nums[j], nums[j - 1] = nums[j - 1], nums[j]
bubble sort
/*bubble sort;timecomplexity=O(n){best case}
time complexity=O(n^2){worst case}
space complexity=O(n);auxiliary space commplexity=O(1)
*/
#include <iostream>
using namespace std;
void swap(int*,int*);
void bubble_sort(int arr[],int n)
{
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(arr[j]>arr[j+1])
{
swap(&arr[j],&arr[j+1]);
}
}
}
}
void display(int arr[],int n)
{
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
int main()
{
int n;
cout<<"enter the size of the array:"<<endl;
cin>>n;
int array_of_numbers[n];
cout<<"enter the elements of the array"<<endl;
for(int i=0;i<n;i++)
{
cin>>array_of_numbers[i];
}
cout<<"array as it was entered:"<<endl;
display(array_of_numbers,n);
bubble_sort(array_of_numbers,n);
cout<<"array after bubble sort:"<<endl;
display(array_of_numbers,n);
return 0;
}
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
bubble sort code
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
}
bubble sort
// I Love Java
import java.io.*;
import java.util.*;
import java.util.stream.*;
import static java.util.stream.Collectors.toList;
public class Bubble_Sort_P {
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
List<Integer> arr = Stream.of(buffer.readLine().replaceAll("\\s+$", " ").split(" ")).map(Integer::parseInt)
.collect(toList());
calculate(arr);
System.out.println(arr);
}
public static void calculate(List<Integer> arr) {
for (int i = 0; i <= arr.size() - 2; i++) {
if (arr.get(i) > arr.get(i + 1)) {
int tem = arr.get(i);
arr.set(i, arr.get(i + 1));
arr.set(i + 1, tem);
calculate(arr);
}
}
}
}
bubble sort algorithm
#include <bits/stdc++.h>
using namespace std;
void bubbleSort(int arr[], int n){
int temp,i,j;
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(arr[i] > arr[j]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main(){
int arr[] = {1,7,33,9,444,2,6,33,69,77,22,9,3,11,5,2,77,3};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
for(int i=0;i<n;i++){
cout << arr[i] << " ";
}
return 0;
}
Algorithm of bubble sort
begin BubbleSort(list)
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
end BubbleSort
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us