From 158da5e7af1d1738690df366dd72fddb642b44d4 Mon Sep 17 00:00:00 2001 From: AselIndula <35985674+AselIndula@users.noreply.github.com> Date: Fri, 5 Oct 2018 12:16:52 +0530 Subject: [PATCH 1/2] SelectionSort --- Selection.mm | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Selection.mm diff --git a/Selection.mm b/Selection.mm new file mode 100644 index 0000000..24415fb --- /dev/null +++ b/Selection.mm @@ -0,0 +1,47 @@ +// C program for implementation of selection sort +#include + +void swap(int *xp, int *yp) +{ + int temp = *xp; + *xp = *yp; + *yp = temp; +} + +void selectionSort(int arr[], int n) +{ + int i, j, min_idx; + + // One by one move boundary of unsorted subarray + for (i = 0; i < n-1; i++) + { + // Find the minimum element in unsorted array + min_idx = i; + for (j = i+1; j < n; j++) + if (arr[j] < arr[min_idx]) + min_idx = j; + + // Swap the found minimum element with the first element + swap(&arr[min_idx], &arr[i]); + } +} + +/* 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, 25, 12, 22, 11}; + int n = sizeof(arr)/sizeof(arr[0]); + selectionSort(arr, n); + printf("Sorted array: \n"); + printArray(arr, n); + return 0; +} \ No newline at end of file From e71b872c3893e715222a4c121f8c8efd32cdf8b2 Mon Sep 17 00:00:00 2001 From: AselIndula <35985674+AselIndula@users.noreply.github.com> Date: Fri, 5 Oct 2018 12:18:57 +0530 Subject: [PATCH 2/2] BubbleSort --- C++/BubbleSort.mm | 41 ++++++++++++++++++++++++++++++++ Selection.mm => C++/Selection.mm | 0 2 files changed, 41 insertions(+) create mode 100644 C++/BubbleSort.mm rename Selection.mm => C++/Selection.mm (100%) diff --git a/C++/BubbleSort.mm b/C++/BubbleSort.mm new file mode 100644 index 0000000..886ab9a --- /dev/null +++ b/C++/BubbleSort.mm @@ -0,0 +1,41 @@ +// C program for implementation of Bubble sort +#include + +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; +} \ No newline at end of file diff --git a/Selection.mm b/C++/Selection.mm similarity index 100% rename from Selection.mm rename to C++/Selection.mm