Bubble Sort
Java
Implementation of Bubble Sort
BubbleSort.java
public class BubbleSort {
public static int[] input;
public static int length;
public static void main(String[] args) {
String inputString = "4143675351981074";
length = inputString.length();
input = new int[length];
for (int i = 0; i < length; i++) {
input[i] = Character.getNumericValue(inputString.charAt(i));
}
System.out.println("Unsorted list:");
printList();
System.out.println();
boolean isFinished = false;
while (!isFinished) {
System.out.println("Iterating:\n");
isFinished = iterate();
System.out.println();
}
System.out.println("Sorted list:");
printList();
}
public static boolean iterate() {
int counter = 0;
for (int i = 0; i < length - 1; i++) {
int temp;
if (input[i] > input[i + 1]) {
counter++;
temp = input[i];
input[i] = input[i + 1];
input[i + 1] = temp;
}
printList();
}
if (counter == 0) {
return true;
}
return false;
}
public static void printList() {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + " ");
}
System.out.println();
}
}
C
Implementation of Bubble Sort
BubbleSort.c
#include <stdio.h>
#include <stdlib.h>
typedef int bool;
#define true 1
#define false 0
bool iterate();
void printList();
int *input;
int length;
int main() {
char inputString[] = "4143675351981074";
length = sizeof(inputString) / sizeof(inputString[0]) - 1;
input = malloc(length * sizeof(int));
input[length];
int i;
for (i = 0; i < length; i++) {
input[i] = inputString[i] - '0';
}
printf("Unsorted list:\n");
printList();
printf("\n");
bool isFinished = false;
while (!isFinished) {
printf("Iterating:\n\n");
isFinished = iterate();
printf("\n");
}
printf("Sorted list:\n");
printList();
return 0;
}
bool iterate() {
int counter = 0;
int i;
for (i = 0; i < length - 1; i++) {
int temp;
if (input[i] > input[i + 1]) {
counter++;
temp = input[i];
input[i] = input[i + 1];
input [i + 1] = temp;
}
printList();
}
if (counter == 0) {
return true;
}
return false;
}
void printList() {
int i;
for (i = 0; i < length; i++) {
printf("%d ", input[i]);
}
printf("\n");
}
C++
Implementation of Bubble Sort
BubbleSort.cpp
#include <iostream>
using namespace std;
bool iterate();
void printList();
int *input;
int length;
int main() {
string inputString = "4143675351981074";
length = inputString.length();
input = new int[length];
int i;
for (i = 0; i < length; i++) {
input[i] = inputString.at(i) - '0';
}
cout << "Unsorted list:\n";
printList();
cout << "\n";
bool isFinished = false;
while (!isFinished) {
cout << "Iterating:\n\n";
isFinished = iterate();
cout << "\n";
}
cout << "Sorted list:\n";
printList();
return 0;
}
bool iterate() {
int counter = 0;
int i;
for (i = 0; i < length - 1; i++) {
int temp;
if (input[i] > input[i + 1]) {
counter++;
temp = input[i];
input[i] = input[i + 1];
input [i + 1] = temp;
}
printList();
}
if (counter == 0) {
return true;
}
return false;
}
void printList() {
int i;
for (i = 0; i < length; i++) {
cout << input[i] << " ";
}
cout << "\n";
}
C#
Implementation of Bubble Sort
BubbleSort.cs
using System;
public class BubbleSort
{
public static int[] input;
public static int length;
public static void Main()
{
String inputString = "4143675351981074";
length = inputString.Length;
input = new int[length];
for (int i = 0; i < length; i++)
{
input[i] = (int)Char.GetNumericValue(inputString[i]);
}
Console.WriteLine("Unsorted list:");
printList();
Console.WriteLine();
bool isFinished = false;
while (!isFinished)
{
Console.WriteLine("Iterating:\n");
isFinished = iterate();
Console.WriteLine();
}
Console.WriteLine("Sorted list:");
printList();
}
public static bool iterate()
{
int counter = 0;
for (int i = 0; i < length - 1; i++)
{
int temp;
if (input[i] > input[i + 1])
{
counter++;
temp = input[i];
input[i] = input[i + 1];
input[i + 1] = temp;
}
printList();
}
if (counter == 0)
{
return true;
}
return false;
}
public static void printList()
{
for (int i = 0; i < input.Length; i++)
{
Console.Write(input[i] + " ");
}
Console.WriteLine();
}
}
Python
Implementation of Bubble Sort
BubbleSort.py
import sys
import array
class BubbleSort:
input_array = [];
length = 0;
def main(self):
inputString = '4143675351981074'
self.length = len(inputString)
for i in range(0, self.length):
self.input_array.append(inputString[i])
print 'Unsorted list:'
self.printList()
print '\n'
isFinished = False;
while (not isFinished):
print 'Iterating:\n'
isFinished = self.iterate();
print '\n'
print 'Sorted list:'
self.printList()
def iterate(self):
counter = 0
for i in range(0, self.length - 1):
temp = None
if (self.input_array[i] > self.input_array[i + 1]):
counter += 1
temp = self.input_array[i]
self.input_array[i] = self.input_array[i + 1]
self.input_array[i + 1] = temp
self.printList()
if (counter == 0):
return True
return False
def printList(self):
for i in range(0, self.length):
sys.stdout.write(str(self.input_array[i]) + ' ')
print ''
bubbleSort = BubbleSort()
bubbleSort.main()
Console Output
Implementation of Bubble Sort
Unsorted list:
4 1 4 3 6 7 5 3 5 1 9 8 1 0 7 4
Iterating:
1 4 4 3 6 7 5 3 5 1 9 8 1 0 7 4
1 4 4 3 6 7 5 3 5 1 9 8 1 0 7 4
1 4 3 4 6 7 5 3 5 1 9 8 1 0 7 4
1 4 3 4 6 7 5 3 5 1 9 8 1 0 7 4
1 4 3 4 6 7 5 3 5 1 9 8 1 0 7 4
1 4 3 4 6 5 7 3 5 1 9 8 1 0 7 4
1 4 3 4 6 5 3 7 5 1 9 8 1 0 7 4
1 4 3 4 6 5 3 5 7 1 9 8 1 0 7 4
1 4 3 4 6 5 3 5 1 7 9 8 1 0 7 4
1 4 3 4 6 5 3 5 1 7 9 8 1 0 7 4
1 4 3 4 6 5 3 5 1 7 8 9 1 0 7 4
1 4 3 4 6 5 3 5 1 7 8 1 9 0 7 4
1 4 3 4 6 5 3 5 1 7 8 1 0 9 7 4
1 4 3 4 6 5 3 5 1 7 8 1 0 7 9 4
1 4 3 4 6 5 3 5 1 7 8 1 0 7 4 9
Iterating:
1 4 3 4 6 5 3 5 1 7 8 1 0 7 4 9
1 3 4 4 6 5 3 5 1 7 8 1 0 7 4 9
1 3 4 4 6 5 3 5 1 7 8 1 0 7 4 9
1 3 4 4 6 5 3 5 1 7 8 1 0 7 4 9
1 3 4 4 5 6 3 5 1 7 8 1 0 7 4 9
1 3 4 4 5 3 6 5 1 7 8 1 0 7 4 9
1 3 4 4 5 3 5 6 1 7 8 1 0 7 4 9
1 3 4 4 5 3 5 1 6 7 8 1 0 7 4 9
1 3 4 4 5 3 5 1 6 7 8 1 0 7 4 9
1 3 4 4 5 3 5 1 6 7 8 1 0 7 4 9
1 3 4 4 5 3 5 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