Skip to content

Commit 9860631

Browse files
committed
rename solved
1 parent d01f635 commit 9860631

20 files changed

+549
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
>>,[>>,]<<[
2+
[<<]>>>>[
3+
<<[>+<<+>-]
4+
>>[>+<<<<[->]>[<]>>-]
5+
<<<[[-]>>[>+<-]>>[<<<+>>>-]]
6+
>>[[<+>-]>>]<
7+
]<<[>>+<<-]<<
8+
]>>>>[.>>]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// C program for implementation of Bubble sort
2+
#include <stdio.h>
3+
4+
void swap(int *xp, int *yp)
5+
{
6+
int temp = *xp;
7+
*xp = *yp;
8+
*yp = temp;
9+
}
10+
11+
// A function to implement bubble sort
12+
void bubbleSort(int arr[], int n)
13+
{
14+
int i, j;
15+
for (i = 0; i < n-1; i++)
16+
17+
// Last i elements are already in place
18+
for (j = 0; j < n-i-1; j++)
19+
if (arr[j] > arr[j+1])
20+
swap(&arr[j], &arr[j+1]);
21+
}
22+
23+
/* Function to print an array */
24+
void printArray(int arr[], int size)
25+
{
26+
int i;
27+
for (i=0; i < size; i++)
28+
printf("%d ", arr[i]);
29+
printf("n");
30+
}
31+
32+
// Driver program to test above functions
33+
int main()
34+
{
35+
int arr[] = {64, 34, 25, 12, 22, 11, 90};
36+
int n = sizeof(arr)/sizeof(arr[0]);
37+
bubbleSort(arr, n);
38+
printf("Sorted array: \n");
39+
printArray(arr, n);
40+
return 0;
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void bubbleSort(int A[],int n)
5+
{
6+
int i,j;
7+
int temp;
8+
for(i=0;i<n;i++)
9+
{
10+
for(j=0;j<n-i;j++)
11+
{
12+
if(A[j]>A[j+1])
13+
{
14+
temp=A[j];
15+
A[j]=A[j+1];
16+
A[j+1]=temp;
17+
}
18+
}
19+
}
20+
}
21+
22+
int main()
23+
{
24+
int n;
25+
cout<<"Enter the size of the array\n";
26+
cin>>n;
27+
int a[n];
28+
cout<<"Enter the element\n";
29+
for(int i=0;i<n;i++)
30+
{
31+
cin>>a[i];
32+
}
33+
bubbleSort(a,n);
34+
cout<<"The Sorted array is :-";
35+
for(int i=0;i<n;i++)
36+
{
37+
cout<<a[i]<<" ";
38+
}
39+
cout<<endl;
40+
return 0;
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void bubbleSort(int A[],int n)
5+
{
6+
int i,j;
7+
int temp;
8+
for(i=0;i<n-1;i++)
9+
{
10+
for(j=0;j<n-i-1;j++)
11+
{
12+
if(A[j]<A[j+1])
13+
{
14+
temp=A[j];
15+
A[j]=A[j+1];
16+
A[j+1]=temp;
17+
}
18+
}
19+
}
20+
}
21+
22+
int main()
23+
{
24+
int n;
25+
cout<<"Enter the size of the array\n";
26+
cin>>n;
27+
int a[n];
28+
cout<<"Enter the element\n";
29+
for(int i=0;i<n;i++)
30+
{
31+
cin>>a[i];
32+
}
33+
bubbleSort(a,n);
34+
cout<<"The Sorted array is :-";
35+
for(int i=0;i<n;i++)
36+
{
37+
cout<<a[i]<<" ";
38+
}
39+
cout<<endl;
40+
return 0;
41+
}

Sorting/Bubble Sort/C/Bubble_Sort_1.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <stdio.h>
2+
3+
void swap(int *xp, int *yp)
4+
{
5+
int temp = *xp;
6+
*xp = *yp;
7+
*yp = temp;
8+
}
9+
10+
// A function to implement bubble sort
11+
void bubbleSort(int arr[], int n)
12+
{
13+
int i, j;
14+
for (i = 0; i < n-1; i++)
15+
16+
// Last i elements are already in place
17+
for (j = 0; j < n-i-1; j++)
18+
if (arr[j] > arr[j+1])
19+
swap(&arr[j], &arr[j+1]);
20+
}
21+
22+
/* Function to print an array */
23+
void printArray(int arr[], int size)
24+
{
25+
int i;
26+
for (i=0; i < size; i++)
27+
printf("%d ", arr[i]);
28+
printf("n");
29+
}
30+
31+
// Driver program to test above functions
32+
int main()
33+
{
34+
int arr[] = {64, 34, 25, 12, 22, 11, 90};
35+
int n = sizeof(arr)/sizeof(arr[0]);
36+
bubbleSort(arr, n);
37+
printf("Sorted array: \n");
38+
printArray(arr, n);
39+
return 0;
40+
}

Sorting/Bubble Sort/C/Bubble_Sort_2.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <stdio.h>
2+
3+
int main()
4+
{
5+
int array[100], n, c, d, swap;
6+
7+
printf("Enter number of elements\n");
8+
scanf("%d", &n);
9+
10+
printf("Enter %d integers\n", n);
11+
12+
for (c = 0; c < n; c++)
13+
scanf("%d", &array[c]);
14+
15+
for (c = 0 ; c < n - 1; c++)
16+
{
17+
for (d = 0 ; d < n - c - 1; d++)
18+
{
19+
if (array[d] > array[d+1]) /* For decreasing order use < */
20+
{
21+
swap = array[d];
22+
array[d] = array[d+1];
23+
array[d+1] = swap;
24+
}
25+
}
26+
}
27+
28+
printf("Sorted list in ascending order:\n");
29+
30+
for (c = 0; c < n; c++)
31+
printf("%d\n", array[c]);
32+
33+
return 0;
34+
}

Sorting/Bubble Sort/Go/Bubble_Sort.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
var toBeSorted [10]int = [10]int{1,3,2,4,8,6,7,2,3,0}
8+
9+
func bubbleSort(input [10]int) {
10+
// n is the number of items in our list
11+
n := 10
12+
swapped := true
13+
for swapped {
14+
swapped = false
15+
for i := 1; i < n-1; i++ {
16+
if input[i-1] > input[i] {
17+
fmt.Println("Swapping")
18+
// swap values using Go's tuple assignment
19+
input[i], input[i-1] = input[i-1], input[i]
20+
swapped = true
21+
}
22+
}
23+
}
24+
fmt.Println(input)
25+
}
26+
27+
28+
func main() {
29+
fmt.Println("Hello World")
30+
bubbleSort(toBeSorted)
31+
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.Arrays;
2+
3+
public class BubbleSort {
4+
/**
5+
* The bubble sort algorithm iterates over the array multiple times, pushing big
6+
* elements to the right by swapping adjacent elements, until the array is
7+
* sorted.
8+
*/
9+
public static void bubbleSort(int[] array, int leftIndex, int rightIndex) {
10+
for (int i = leftIndex; i < rightIndex; i++) {
11+
for (int j = leftIndex; j < rightIndex; j++) {
12+
if (array[j] > array[j + 1]) {
13+
int aux = array[j];
14+
array[j] = array[j + 1];
15+
array[j + 1] = aux;
16+
}
17+
}
18+
}
19+
}
20+
/**
21+
* Main Method
22+
*/
23+
public static void main(String[] args) {
24+
int[] array = { 8, 2, 3, 4, 1, 5, 7, 6 };
25+
bubbleSort(array, 0, array.length - 1);
26+
System.out.println(Arrays.toString(array));
27+
}
28+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
7+
import java.util.Scanner;
8+
9+
/**
10+
*
11+
* @author Yudi Setiawan
12+
*
13+
* Bubble Sort
14+
*
15+
*/
16+
17+
public class BubleSort
18+
{
19+
public static void main(String[] args)
20+
{
21+
Scanner s = new Scanner(System.in);
22+
23+
System.out.print("Input data n : "); int n_data = s.nextInt();
24+
25+
int[] data = new int[n_data];
26+
System.out.println();
27+
for(int a = 0; a < n_data; a++)
28+
{
29+
System.out.print("Data -"+(a+1)+" : ");
30+
data[a] = s.nextInt();
31+
}
32+
33+
System.out.println("\nData before sorted");
34+
for(int a = 0; a < n_data; a++)
35+
System.out.print(data[a]+" ");
36+
37+
// Proses Bubble Sort
38+
System.out.println("\nProcces Bubble Sort");
39+
for(int a = 0; a < n_data; a++)
40+
{
41+
System.out.println("Iterasi -"+(a+1)+" :");
42+
for(int b = 0; b < n_data; b++)
43+
System.out.print(data[b]+" ");
44+
45+
System.out.println(" Compare "+data[0]+" with "+data[1]);
46+
for(int b = 0; b < n_data-1; b++)
47+
{
48+
String Message = " There is no sorting";
49+
if(data[b] > data[b+1])
50+
{
51+
Message = " Data "+data[b]+" switch with "+data[b+1];
52+
int temp = data[b];
53+
data[b] = data[b+1];
54+
data[b+1] = temp;
55+
56+
}
57+
58+
if(b < n_data-(a+1))
59+
{
60+
for(int c = 0; c < n_data; c++)
61+
System.out.print(data[c]+" ");
62+
63+
System.out.println(Message);;
64+
}
65+
}
66+
67+
System.out.println("\n");
68+
}
69+
70+
System.out.print("Data after Sorted : ");
71+
for(int a = 0; a < n_data; a++)
72+
System.out.print(data[a]+" ");
73+
74+
}
75+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.Arrays;
2+
3+
public class OptimizedBubbleSort {
4+
static void sort(int [] arrA){
5+
if(arrA==null || arrA.length==0)
6+
return;
7+
8+
System.out.println("Original Array: " + Arrays.toString(arrA));
9+
boolean isSwapped;
10+
int size = arrA.length;
11+
for (int i = 0; i <size-1 ; i++) {
12+
isSwapped = false;
13+
for (int j = 0; j <size-i-1 ; j++) {
14+
//check the adjacent elements
15+
if(arrA[j]>arrA[j+1]){
16+
//swap the elements
17+
int temp = arrA[j];
18+
arrA[j] = arrA[j+1];
19+
arrA[j+1] = temp;
20+
isSwapped = true;
21+
}
22+
}
23+
//check if any swapping occurred in last iteration
24+
//if yes then break the loop, array is sorted at this point
25+
if(isSwapped==false)
26+
break;
27+
}
28+
System.out.println("Sorted Array: " + Arrays.toString(arrA));
29+
}
30+
31+
public static void main(String[] args) {
32+
int [] arrA = {5, 1, 9, 3, 2, 10};
33+
sort(arrA);
34+
System.out.println("------------------------------");
35+
arrA = new int []{1, 2, 4, 6, 8, 10};
36+
sort(arrA);
37+
}
38+
}

0 commit comments

Comments
 (0)