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
// 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
class Sort
{
static void bubbleSort(int arr[], int n)
{
if (n == 1) //passes are done
{
return;
}
for (int i=0; i<n-1; i++) //iteration through unsorted elements
{
if (arr[i] > arr[i+1]) //check if the elements are in order
{ //if not, swap them
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
bubbleSort(arr, n-1); //one pass done, proceed to the next
}
void display(int arr[]) //display the array
{
for (int i=0; i<arr.length; ++i)
{
System.out.print(arr[i]+" ");
}
}
public static void main(String[] args)
{
Sort ob = new Sort();
int arr[] = {6, 4, 5, 12, 2, 11, 9};
bubbleSort(arr, arr.length);
ob.display(arr);
}
}
Bubble Sort
#include<stdio.h>
void main ()
{
int i, j,temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] > a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Printing Sorted Element List ...\n");
for(i = 0; i<10; i++)
{
printf("%d\n",a[i]);
}
}
Bubble Sort
import static java.lang.Integer.parseInt;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.StringTokenizer;
public class Day20_Sorting {
static int MB = 1 << 20;
static BufferedReader BR = new BufferedReader( new InputStreamReader(System.in, StandardCharsets.US_ASCII), 20 * MB);
static StringTokenizer st;
static String lastLine;
static void newLine() throws IOException {
lastLine = BR.readLine();
st = new StringTokenizer(lastLine);
}
public static void main(String[] args) throws IOException {
newLine();
int N = parseInt(st.nextToken());
newLine();
int[] A = new int[N];
for (int i = 0; i < N; i++) {
A[i] = parseInt(st.nextToken());
}
int numberOfSwapps = bubbleSort(N, A);
int firstElement = A[0];
int lastElement = A[N-1];
print(numberOfSwapps, firstElement, lastElement);
}
private static void print(int numberOfSwapps, int firstElement, int lastElement) {
StringBuilder sb = new StringBuilder();
sb.append("Array is sorted in ").append(numberOfSwapps).append(" swaps.\n");
sb.append("First Element: ").append(firstElement).append('\n');
sb.append("Last Element: ").append(lastElement).append('\n');
System.out.print(sb);
}
private static int bubbleSort(int N, int[] A) {
int cnt = 0;
for (int i = 0; i < N; i++) {
// Track number of elements swapped during a single array traversal
int numberOfSwaps = 0;
for (int j = 0; j < N - 1; j++) {
// Swap adjacent elements if they are in decreasing order
if (A[j] > A[j + 1]) {
swap(A, j , j + 1);
numberOfSwaps++;
}
}
cnt += numberOfSwaps;
// If no elements were swapped during a traversal, array is sorted
if (numberOfSwaps == 0) {
break;
}
}
return cnt;
}
private static void swap(int[] a, int i, int j) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
Bubble sort
using System;
public class Bubble_Sort
{
public static void Main(string[] args)
{
int[] a = { 3, 0, 2, 5, -1, 4, 1 };
int t;
Console.WriteLine("Original array :");
foreach (int aa in a)
Console.Write(aa + " ");
for (int p = 0; p <= a.Length - 2; p++)
{
for (int i = 0; i <= a.Length - 2; i++)
{
if (a[i] > a[i + 1])
{
t = a[i + 1];
a[i + 1] = a[i];
a[i] = t;
}
}
}
Console.WriteLine("\n"+"Sorted array :");
foreach (int aa in a)
Console.Write(aa + " ");
Console.Write("\n");
}
}
bubble sort
#include<iostream>
using namespace std;
int main ()
{
int i, j,temp;
int a[5] = {10,2,0,43,12};
cout <<"Input list ...\n";
for(i = 0; i<5; i++) {
cout <<a[i]<<"\t";
}
cout<<endl;
for(i = 0; i<5; i++) {
for(j = i+1; j<5; j++)
{
if(a[j] < a[i]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
cout <<"Sorted Element List ...\n";
for(i = 0; i<5; i++) {
cout <<a[i]<<"\t";
}
return 0;
}
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