From f6f985db41122a05802dd320e359a5888752485a Mon Sep 17 00:00:00 2001 From: Igor Pereira Date: Mon, 14 Oct 2019 23:45:57 -0300 Subject: [PATCH 1/4] add insertionSort.cpp --- sort/insertionSort.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 sort/insertionSort.cpp diff --git a/sort/insertionSort.cpp b/sort/insertionSort.cpp new file mode 100644 index 0000000..1f71c9f --- /dev/null +++ b/sort/insertionSort.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; +void printArray (int arg[], int length) { + for (int n = 0; n < length; ++n) + cout << arg[n] << ' '; + cout << '\n'; +} + + +void insertionSort(int array [], int leftIndex, int rightIndex){ + for (int i = leftIndex+1; i <= rightIndex; i++){ + int key = array[i]; + int j = i-1; + while(j>= leftIndex && array[j] > key){ + array[j+1] = array[j]; + j--; + } + array[j+1] = key; + } + printArray(array,rightIndex); +} + +int main(){ + int array [] = {9,3,8,2, 4 , 1, 7 ,6,10}; + int size = sizeof(array) / sizeof(array[0]); + insertionSort(array, 0 , size); +} From 1440ddc59ab6c9f1b4bb30c06b56b67f377cd2bd Mon Sep 17 00:00:00 2001 From: Igor Pereira Date: Tue, 15 Oct 2019 00:40:29 -0300 Subject: [PATCH 2/4] ADD RECURSIVEBUBBLESORT --- sort/recursiveBubbleSort.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 sort/recursiveBubbleSort.cpp diff --git a/sort/recursiveBubbleSort.cpp b/sort/recursiveBubbleSort.cpp new file mode 100644 index 0000000..e16b163 --- /dev/null +++ b/sort/recursiveBubbleSort.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + + +void bubbleSort(int array [], int rightIndex) { + if (rightIndex == 1) + return; + + for (int i = 0; i < rightIndex-1; i++) + if (array[i] > array[i+1]) + swap(array[i], array[i+1]); + + bubbleSort(array, rightIndex-1); +} + +void printArray(int array[], int rightIndex) { + for (int i = 0; i < rightIndex; i++) + cout << array[i] << " "; + cout << "" << endl; +} + +int main() { + int array[] = {3,77,2, 34, 25, 11, 12, 9, 7}; + int lenght = sizeof(array)/sizeof(array[0]); + bubbleSort(array, lenght); + printArray(array, lenght); +} \ No newline at end of file From c5fa1d4944d293e6c7e4fce9467dc164c9a82827 Mon Sep 17 00:00:00 2001 From: pereiraIgor Date: Tue, 15 Oct 2019 10:00:35 -0300 Subject: [PATCH 3/4] ADD INSERTION SORT --- sort/recursiveBubbleSort.cpp | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 sort/recursiveBubbleSort.cpp diff --git a/sort/recursiveBubbleSort.cpp b/sort/recursiveBubbleSort.cpp deleted file mode 100644 index e16b163..0000000 --- a/sort/recursiveBubbleSort.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include - -using namespace std; - - -void bubbleSort(int array [], int rightIndex) { - if (rightIndex == 1) - return; - - for (int i = 0; i < rightIndex-1; i++) - if (array[i] > array[i+1]) - swap(array[i], array[i+1]); - - bubbleSort(array, rightIndex-1); -} - -void printArray(int array[], int rightIndex) { - for (int i = 0; i < rightIndex; i++) - cout << array[i] << " "; - cout << "" << endl; -} - -int main() { - int array[] = {3,77,2, 34, 25, 11, 12, 9, 7}; - int lenght = sizeof(array)/sizeof(array[0]); - bubbleSort(array, lenght); - printArray(array, lenght); -} \ No newline at end of file From b5ede3d9e1c157d393f63eff8824787c5c304019 Mon Sep 17 00:00:00 2001 From: Igor Pereira Date: Tue, 15 Oct 2019 12:43:17 -0300 Subject: [PATCH 4/4] ADD recusive BinarySearch --- utils/recursiveBinarySearch.cpp | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 utils/recursiveBinarySearch.cpp diff --git a/utils/recursiveBinarySearch.cpp b/utils/recursiveBinarySearch.cpp new file mode 100644 index 0000000..c424c6d --- /dev/null +++ b/utils/recursiveBinarySearch.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int binarySearch(int array [], int leftIndex, int rightIndex, int toSearch){ + if(leftIndex > rightIndex) + return -1; + + int middle = leftIndex + (rightIndex - leftIndex ) / 2; + + if(array[middle] == toSearch){ + return middle; + } + if(array[middle] > toSearch){ + return binarySearch(array, leftIndex, middle - 1 ,toSearch); + } + + return binarySearch(array, middle + 1,rightIndex ,toSearch); + +} + + +int main(){ + int array[] = {30, 82 ,74 ,26 ,99 ,26 ,3 ,82 ,54 ,4 ,28 ,1 ,88 ,89 ,66 ,69 ,83 ,58 , 6, + 31 ,13 ,47 ,59 ,85 ,7, 20 ,22 ,48 ,77 ,92 ,52 ,2 ,92 ,15 ,94 ,43 ,37 ,64 ,13 ,83 ,13 ,62 }; + int toSearch; + int n = sizeof(array)/sizeof(array[0]); + cout << "number to seach" << endl; + + cin >> toSearch; + + sort(array, array + n); + + cout << "in positon = "<< binarySearch(array,0, n - 1 , toSearch) << endl; +} \ No newline at end of file