xxxxxxxxxx
procedure bubbleSort(arr: list of comparable items)
n = length(arr)
do
swapped = false
for i from 1 to n-1 inclusive do
if arr[i-1] > arr[i] then
swap(arr[i-1], arr[i])
swapped = true
end if
end for
n = n - 1
while swapped is true
end procedure
In this pseudocode:
arr is the input array that needs to be sorted.
length(arr) gives the number of elements in the array.
The outer loop (while swapped is true) continues until no more swaps are needed.
The inner loop (for i from 1 to n-1 inclusive) goes through the array, compares adjacent elements, and swaps them if they are in the wrong order.
The swapped variable is used to track whether any swaps were made in a pass through the array. If no swaps are made, the array is considered sorted, and the algorithm terminates.
Note: The pseudocode uses 1-based indexing for the array. Adjustments may be needed if your programming language uses 0-based indexing. Additionally, the swap function is used to exchange the values of two elements.
xxxxxxxxxx
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] + " ");
}
}
}
xxxxxxxxxx
// 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;
}
xxxxxxxxxx
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]))
xxxxxxxxxx
#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]
xxxxxxxxxx
bubbleSort(array)
for i <- 1 to indexOfLastUnsortedElement-1
if leftElement > rightElement
swap leftElement and rightElement
end bubbleSort
xxxxxxxxxx
function bubbleSort(arr) {
// This variable is used to either continue or stop the loop
let continueSorting = true;
// while continueSorting is true
while (continueSorting) {
// setting continueSorting false. below check to see if swap,
// if a swap,continue sorting. If no swaps, done sorting,
// and stop our while loop.
continueSorting = false;
// loop through the arr from 0 to next to last
for (let i = 0; i < arr.length - 1; i++) {
// check if we need to swap
if (arr[i] > arr[i + 1]) {
// swap
let temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
// since a swap was made, we want to continue sorting
continueSorting = true;
}
}
}
// After all swaps have been made, then we return our sorted array
return arr;
xxxxxxxxxx
int *BubbleSort(int ar[], int size)
{
bool sorted = false;
while (sorted == false)
{
sorted = true;
for (int i = 0; i < size; i++)
{
if (ar[i] > ar[i + 1])
{
swap(ar[i], ar[i + 1]);
sorted = false;
}
}
}
return ar;
}
xxxxxxxxxx
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));
}
}
xxxxxxxxxx
#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;
}
}
xxxxxxxxxx
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
}