728x90
반응형
#include <stdio.h>
# define MAX 10

int* bubble_sort(int arr[], int n) {
    int i, j, temp;
    for (i = n - 1; i > 0; i--) {
        for (j = 0; j < i; j++) {
            if (arr[j] > arr[j + 1]) {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
    return arr;

}

int main() {
    int arr[MAX] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
    int* arr_new = bubble_sort(arr, MAX);
    for (int i = 0; i < MAX; i++) {
        printf("%d ", *(arr_new + i));
    }
    return 0;
}

/*
2 4 6 8 10 12 37 45 68 89
*/
// 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;
}
# PYTHON

def bubbleSort(x):
	length = len(x)-1
	for i in range(length):
		for j in range(length-i):
			if x[j] > x[j+1]:
				x[j], x[j+1] = x[j+1], x[j]
	return x
# Python program for implementation of Bubble Sort
 
 
def bubbleSort(arr):
    n = len(arr)
 
    # Traverse through all array elements
    for i in range(n):
 
        # Last i elements are already in place
        for j in range(0, n-i-1):
 
            # traverse the array from 0 to n-i-1
            # Swap if the element found is greater
            # than the next element
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
 
 
# Driver code to test above
if __name__ == "__main__":
  arr = [64, 34, 25, 12, 22, 11, 90]
 
  bubbleSort(arr)
 
  print("Sorted array is:")
  for i in range(len(arr)):
      print("%d" % arr[i], end=" ")
/*
// Bubble Sort

The concept is simple and easy to program
because it is arranged in a way that compares two adjacent items.

However, the disadvantage is that there are so many comparison tasks that
it takes a long time to calculate.
*/

#include <stdio.h>
#define MAX_COUNT 5

void BubbleSort(int a_data[], int a_count);

int main(void)
{
	int data[MAX_COUNT] = { 7, 4, 11, 9, 2 };
	int i;

	printf("[ Before sorting ]\n\n");
	for (i = 0; i < MAX_COUNT; i++)
	{
		printf("%d ", data[i]);
	}

	BubbleSort(data, MAX_COUNT);

	printf("[ After sorting ]\n\n");
	for (i = 0; i < MAX_COUNT; i++)
	{
		printf("%d ", data[i]);
	}

	return 0;
}

void BubbleSort(int a_data[], int a_count)
{
	int step, i, temp = 0;
	int comparison_cnt = 0;

	for (step = 0; step < a_count - 1; step++)
	{
		for (i = 0; i < a_count - 1 - step; i++)
		{
			comparison_cnt++;

			if (a_data[i] > a_data[i + 1])
			{
				temp = a_data[i];
				a_data[i] = a_data[i + 1];
				a_data[i + 1] = temp;
			}
		}
	}
	printf("\n\n\n< Total number of comparison : %d >\n\n\n", comparison_cnt);
}

/*
[ Before sorting ]

7 4 11 9 2


< Total number of comparison : 10 >


[ After sorting ]

2 4 7 9 11
*/
#include <stdio.h>
#define MAX_COUNT 10

void BubbleSort(int a_data[], int a_count);

int main(void)
{
	int data[MAX_COUNT] = { 1, 7, 2, 4, 6, 9, 3, 12, 11, 10 };
	int i;

	printf("[ Before sorting ]\n\n");
	for (i = 0; i < MAX_COUNT; i++)
	{
		printf("%d ", data[i]);
	}

	BubbleSort(data, MAX_COUNT);

	printf("[ After sorting ]\n\n");
	for (i = 0; i < MAX_COUNT; i++)
	{
		printf("%d ", data[i]);
	}

	return 0;
}

void BubbleSort(int a_data[], int a_count)
{
	int step, i, temp = 0, flag;
	int comparison_cnt = 0;

	for (step = 0; step < a_count - 1; step++)
	{
		flag = 1;

		for (i = 0; i < a_count - 1 - step; i++)
		{
			comparison_cnt++;

			if (a_data[i] > a_data[i + 1])
			{
				flag = 0;
				temp = a_data[i];
				a_data[i] = a_data[i + 1];
				a_data[i + 1] = temp;
			}
		}
		if (flag == 1) break;
	}
	printf("\n\n\n< Total number of comparison : %d >\n\n\n", comparison_cnt);
}

/*
[ Before sorting ]

1 7 2 4 6 9 3 12 11 10


< Total number of comparison : 35 >


[ After sorting ]

1 2 3 4 6 7 9 10 11 12
*/
728x90
반응형

'Data Structure & Algorithm' 카테고리의 다른 글

Simple Linked List(+dummy)  (0) 2022.09.19
Linked List  (0) 2022.09.10
Sequential Data Structure  (0) 2022.09.07
Recursion (Factorial, Fibonacci, Tower of Hanoi)  (0) 2022.09.06
Binary Search Algorithm  (0) 2022.09.05

+ Recent posts