Sorting Algorithm
Awaten yu lattan, In rush. Para makita lang iteration na etoy. Ti agriri kupal.unknown
c_cpp
a month ago
3.7 kB
29
Indexable
Never
#include <iostream> using namespace std; int quickStep = 1; int partition(int array[], int low, int high) { int pivot = array[high]; int i = low - 1; for (int j = low; j < high; j++) { if (array[j] < pivot) { i++; int temp = array[i]; array[i] = array[j]; array[j] = temp; } } int temp = array[i + 1]; array[i + 1] = array[high]; array[high] = temp; return i + 1; } void printIteration(int array[], int size, int pos) { cout << pos << ". "; for (int n = 0; n < size; n++) { cout << array[n] << " "; } cout << endl; } void bubbleSort(int array[], int size) { cout << "\n\nIteration: " << endl; for (int i = 0; i < size - 1; i++) { printIteration(array, size, i + 1); for (int j = 0; j < size - i - 1; j++) { if (array[j] > array[j + 1]) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } } void selectionSort(int array[], int size) { int min_index = 0; cout << "\n\nIteration: " << endl; for (int i = 0; i < size - 1; i++) { printIteration(array, size, i + 1); min_index = i; for (int j = i + 1; j < size; j++) { if (array[j] < array[min_index]) { min_index = j; } } int temp = array[i]; array[i] = array[min_index]; array[min_index] = temp; } } void insertionSort(int array[], int size) { int min_index = 0; cout << "\n\nIteration: " << endl; for (int i = 1; i < size; i++) { int n = array[i]; int j = i - 1; while (j >= 0 && array[j] > n) { array[j + 1] = array[j]; j = j - 1; } array[j + 1] = n; printIteration(array, size, i); } } void quickSort(int array[], int low, int high) { if (low < high) { int pivot_index = partition(array, low, high); printIteration(array, high + 1, quickStep); quickStep++; quickSort(array, low, pivot_index - 1); quickSort(array, pivot_index + 1, high); } } void printArray(int array[], int size) { for (int i = 0; i < size; i++) { cout << array[i] << " "; } } int callSelectionSort() { int array[] = { 9, 3, 11, 15, 12, 4 }; int size = sizeof(array) / sizeof(array[0]); cout << "\n\n\n== SELECTION SORT ==" << endl; cout << "Unsorted Array: "; printArray(array, size); selectionSort(array, size); cout << "\nSorted Array: "; printArray(array, size); } int callBubbleSort() { int array[] = { 9, 3, 11, 15, 12, 4 }; int size = sizeof(array) / sizeof(array[0]); cout << "== BUBBLE SORT ==" << endl; cout << "Unsorted Array: "; printArray(array, size); bubbleSort(array, size); cout << "\nSorted Array: "; printArray(array, size); } int callInsertionSort() { int array[] = { 9, 3, 11, 15, 12, 4 }; int size = sizeof(array) / sizeof(array[0]); cout << "\n\n\n== INSERTION SORT ==" << endl; cout << "Unsorted Array: "; printArray(array, size); insertionSort(array, size); cout << "\nSorted Array: "; printArray(array, size); } int callQuickSort() { int array[] = { 9, 3, 11, 15, 12, 4 }; int size = sizeof(array) / sizeof(array[0]); cout << "\n\n\n== QUICK SORT ==" << endl; cout << "Unsorted Array: "; printArray(array, size); cout << "\n\nIteration: " << endl; quickSort(array, 0, size - 1); cout << "\nSorted Array: "; printArray(array, size); } int main() { cout << "=== SORTING ALGORITHMS ===" << endl << endl; callBubbleSort(); callSelectionSort(); callInsertionSort(); callQuickSort(); return 0; }
Leave a Comment