diff --git a/Algorithms/Bubblesort/Kotlin/BubbleSort.kt b/Algorithms/Bubblesort/Kotlin/BubbleSort.kt deleted file mode 100644 index d8537e00..00000000 --- a/Algorithms/Bubblesort/Kotlin/BubbleSort.kt +++ /dev/null @@ -1,27 +0,0 @@ -import java.util.* -fun bubbleSort(arr:IntArray){ - for(i in 0 until arr.size-1){ - for(j in 0 until arr.size-i-1){ - if(arr[j]>arr[j+1]){ - var temp = arr[j] - arr[j] = arr[j+1] - arr[j+1] = temp - } - } - } -} -fun main(args:Array) { - var scanner = Scanner(System.`in`) - print("enter the size to be sorted ") - var sizeOfList = scanner.nextInt() - val array = IntArray(sizeOfList) - print("start entering values to be sorted ") - for (i in 0 until sizeOfList) { - array[i] = scanner.nextInt(); - } - bubbleSort(array) - for (i in 0 until sizeOfList){ - print(array[i]) - print(" ") - } -} \ No newline at end of file diff --git a/Algorithms/Bubblesort/kotlin/BubbleSort.kt b/Algorithms/Bubblesort/kotlin/BubbleSort.kt deleted file mode 100644 index 12344e45..00000000 --- a/Algorithms/Bubblesort/kotlin/BubbleSort.kt +++ /dev/null @@ -1,18 +0,0 @@ -class DummyBubbleSort { - - private fun sortArray(numbers: Array) { - for (currentPosition in 0 until (numbers.size - 1)) { - if (numbers[currentPosition] > numbers[currentPosition + 1]) { - val tmp = numbers[currentPosition] - numbers[currentPosition] = numbers[currentPosition + 1] - numbers[currentPosition + 1] = tmp - } - } - } - - fun main(args: Array) { - val numbers = arrayOf(69, 42, 666, 1, 969, 23, 6, 2, 0, 87) - sortArray(numbers) - numbers.forEach(::print) - } -} diff --git a/Algorithms/Heapsort/C++/heapsort.cpp b/Algorithms/Heapsort/C++/heapsort.cpp new file mode 100644 index 00000000..3c77003e --- /dev/null +++ b/Algorithms/Heapsort/C++/heapsort.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; + +void heapify(int arr[], int n, int i) { + int largest = i; + int l = 2*i + 1; + int r = 2*i + 2; + + if (l < n && arr[l] > arr[largest]) + largest = l; + + if (r < n && arr[r] > arr[largest]) + largest = r; + + if (largest != i) { + swap(arr[i], arr[largest]); + heapify(arr, n, largest); + } +} + +void heapSort(int arr[], int n) { + for (int i = n / 2 - 1; i >= 0; i--) + heapify(arr, n, i); + + for (int i=n-1; i>=0; i--) { + swap(arr[0], arr[i]); + + heapify(arr, i, 0); + } +} + +int main() { + int n; + cin>>n; + int *arr = new int[n]; + for(int i = 0;i < n;i++) { + cin>>arr[i]; + } + heapSort(arr, n); + for(int i = 0;i < n;i++) { + cout<