1#Bubble Sort
2nums = [9, 4, 5, 1, 3, 7, 6]
3for i in range(len(nums)):
4 for j in range(1, len(nums)):
5 if nums[j] < nums[j - 1]:
6 nums[j], nums[j - 1] = nums[j - 1], nums[j]
1/*bubble sort;timecomplexity=O(n){best case}
2 time complexity=O(n^2){worst case}
3 space complexity=O(n);auxiliary space commplexity=O(1)
4*/
5#include <iostream>
6
7using namespace std;
8void swap(int*,int*);
9void bubble_sort(int arr[],int n)
10{
11 for(int i=0;i<n-1;i++)
12 {
13 for(int j=0;j<n-1-i;j++)
14 {
15 if(arr[j]>arr[j+1])
16 {
17 swap(&arr[j],&arr[j+1]);
18 }
19 }
20 }
21}
22void display(int arr[],int n)
23{
24 for(int i=0;i<n;i++)
25 {
26 cout<<arr[i]<<" ";
27 }
28 cout<<endl;
29}
30
31int main()
32{
33 int n;
34 cout<<"enter the size of the array:"<<endl;
35 cin>>n;
36 int array_of_numbers[n];
37 cout<<"enter the elements of the array"<<endl;
38 for(int i=0;i<n;i++)
39 {
40 cin>>array_of_numbers[i];
41 }
42 cout<<"array as it was entered:"<<endl;
43 display(array_of_numbers,n);
44 bubble_sort(array_of_numbers,n);
45 cout<<"array after bubble sort:"<<endl;
46 display(array_of_numbers,n);
47 return 0;
48}
49void swap(int *a,int *b)
50{
51 int temp=*a;
52 *a=*b;
53 *b=temp;
54}
55
1// I Love Java
2import java.io.*;
3import java.util.*;
4import java.util.stream.*;
5import static java.util.stream.Collectors.toList;
6
7public class Bubble_Sort_P {
8 public static void main(String[] args) throws IOException {
9 BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
10
11 List<Integer> arr = Stream.of(buffer.readLine().replaceAll("\\s+$", " ").split(" ")).map(Integer::parseInt)
12 .collect(toList());
13
14 calculate(arr);
15
16 System.out.println(arr);
17 }
18
19 public static void calculate(List<Integer> arr) {
20 for (int i = 0; i <= arr.size() - 2; i++) {
21 if (arr.get(i) > arr.get(i + 1)) {
22 int tem = arr.get(i);
23 arr.set(i, arr.get(i + 1));
24 arr.set(i + 1, tem);
25 calculate(arr);
26 }
27 }
28 }
29}
1#include<stdio.h>
2void main ()
3{
4 int i, j,temp;
5 int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
6 for(i = 0; i<10; i++)
7 {
8 for(j = i+1; j<10; j++)
9 {
10 if(a[j] > a[i])
11 {
12 temp = a[i];
13 a[i] = a[j];
14 a[j] = temp;
15 }
16 }
17 }
18 printf("Printing Sorted Element List ...\n");
19 for(i = 0; i<10; i++)
20 {
21 printf("%d\n",a[i]);
22 }
23}
1class Sort
2{
3 static void bubbleSort(int arr[], int n)
4 {
5 if (n == 1) //passes are done
6 {
7 return;
8 }
9
10 for (int i=0; i<n-1; i++) //iteration through unsorted elements
11 {
12 if (arr[i] > arr[i+1]) //check if the elements are in order
13 { //if not, swap them
14 int temp = arr[i];
15 arr[i] = arr[i+1];
16 arr[i+1] = temp;
17 }
18 }
19
20 bubbleSort(arr, n-1); //one pass done, proceed to the next
21 }
22
23 void display(int arr[]) //display the array
24 {
25 for (int i=0; i<arr.length; ++i)
26 {
27 System.out.print(arr[i]+" ");
28 }
29 }
30
31 public static void main(String[] args)
32 {
33 Sort ob = new Sort();
34 int arr[] = {6, 4, 5, 12, 2, 11, 9};
35 bubbleSort(arr, arr.length);
36 ob.display(arr);
37 }
38}
39
1import static java.lang.Integer.parseInt;
2
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.InputStreamReader;
6import java.nio.charset.StandardCharsets;
7import java.util.StringTokenizer;
8
9public class Day20_Sorting {
10
11 static int MB = 1 << 20;
12 static BufferedReader BR = new BufferedReader( new InputStreamReader(System.in, StandardCharsets.US_ASCII), 20 * MB);
13
14 static StringTokenizer st;
15 static String lastLine;
16
17 static void newLine() throws IOException {
18 lastLine = BR.readLine();
19 st = new StringTokenizer(lastLine);
20 }
21
22 public static void main(String[] args) throws IOException {
23 newLine();
24 int N = parseInt(st.nextToken());
25
26 newLine();
27 int[] A = new int[N];
28 for (int i = 0; i < N; i++) {
29 A[i] = parseInt(st.nextToken());
30 }
31
32 int numberOfSwapps = bubbleSort(N, A);
33 int firstElement = A[0];
34 int lastElement = A[N-1];
35 print(numberOfSwapps, firstElement, lastElement);
36 }
37
38 private static void print(int numberOfSwapps, int firstElement, int lastElement) {
39 StringBuilder sb = new StringBuilder();
40
41 sb.append("Array is sorted in ").append(numberOfSwapps).append(" swaps.\n");
42 sb.append("First Element: ").append(firstElement).append('\n');
43 sb.append("Last Element: ").append(lastElement).append('\n');
44
45 System.out.print(sb);
46 }
47
48 private static int bubbleSort(int N, int[] A) {
49 int cnt = 0;
50
51 for (int i = 0; i < N; i++) {
52 // Track number of elements swapped during a single array traversal
53 int numberOfSwaps = 0;
54
55 for (int j = 0; j < N - 1; j++) {
56 // Swap adjacent elements if they are in decreasing order
57 if (A[j] > A[j + 1]) {
58 swap(A, j , j + 1);
59 numberOfSwaps++;
60 }
61 }
62 cnt += numberOfSwaps;
63
64 // If no elements were swapped during a traversal, array is sorted
65 if (numberOfSwaps == 0) {
66 break;
67 }
68 }
69
70 return cnt;
71 }
72
73 private static void swap(int[] a, int i, int j) {
74 int tmp = a[i];
75 a[i] = a[j];
76 a[j] = tmp;
77 }
78
79}
1using System;
2public class Bubble_Sort
3{
4 public static void Main(string[] args)
5 {
6 int[] a = { 3, 0, 2, 5, -1, 4, 1 };
7 int t;
8 Console.WriteLine("Original array :");
9 foreach (int aa in a)
10 Console.Write(aa + " ");
11 for (int p = 0; p <= a.Length - 2; p++)
12 {
13 for (int i = 0; i <= a.Length - 2; i++)
14 {
15 if (a[i] > a[i + 1])
16 {
17 t = a[i + 1];
18 a[i + 1] = a[i];
19 a[i] = t;
20 }
21 }
22 }
23 Console.WriteLine("\n"+"Sorted array :");
24 foreach (int aa in a)
25 Console.Write(aa + " ");
26 Console.Write("\n");
27
28 }
29}
30
31