* Някои популярни алгоритми за сортиране, реализирани на Java
Публикувано на 06 декември 2024 в раздел ПИК3 Java.
Кодът по-долу представя основни алгоритми за сортирани. Реализацията им е на Java. Правени са в учебна среда - възможно е да има допуснати неточности и грешки.
static void selectionSort(int[] arr) { int min_index; for (int i = 0; i < arr.length - 1; i++) { min_index = i; for (int j = i + 1; j < arr.length; j++) { if (arr[j] < arr[min_index]) { min_index = j; } } int temp = arr[i]; arr[i] = arr[min_index]; arr[min_index] = temp; } } static void bubbleSortUnoptimized(int arr[]) { for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } static void bubbleSort(int arr[]) { boolean swapDone; for (int i = 0; i < arr.length - 1; i++) { swapDone = false; for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; swapDone = true; } } if (swapDone == false) { return; } } } static void mergeSort(int[] arr) { if (arr.length < 2) { return; } int mid = arr.length / 2; int[] leftPart = new int[mid]; int[] rightPart = new int[arr.length - mid]; for (int i = 0; i < mid; i++) { leftPart[i] = arr[i]; } for (int i = mid; i < arr.length; i++) { rightPart[i - mid] = arr[i]; } mergeSort(leftPart); mergeSort(rightPart); int i = 0, j = 0, k = 0; while (i < mid && j < arr.length - mid) { if (leftPart[i] <= rightPart[j]) { arr[k] = leftPart[i]; k++; i++; } else { arr[k] = rightPart[j]; k++; j++; } } while (i < mid) { arr[k] = leftPart[i]; k++; i++; } while (j < arr.length - mid) { arr[k] = rightPart[j]; k++; j++; } } static void quickSort(int[] array) { class LocalClass { void quickSortRec(int[] arr, int lowIndex, int highIndex) { if (lowIndex >= highIndex) { return; } int pivotIndex = new java.util.Random().nextInt(highIndex - lowIndex) + lowIndex; int pivot = array[pivotIndex]; int temp = array[pivotIndex]; array[pivotIndex] = array[highIndex]; array[highIndex] = temp; int leftPointer = lowIndex; int rightPointer = highIndex - 1; while (leftPointer < rightPointer) { while (array[leftPointer] <= pivot && leftPointer < rightPointer) { leftPointer++; } while (array[rightPointer] >= pivot && leftPointer < rightPointer) { rightPointer--; } temp = array[leftPointer]; array[leftPointer] = array[rightPointer]; array[rightPointer] = temp; } if (array[leftPointer] > array[highIndex]) { temp = array[leftPointer]; array[leftPointer] = array[highIndex]; array[highIndex] = temp; } else { leftPointer = highIndex; } quickSortRec(array, lowIndex, leftPointer - 1); quickSortRec(array, leftPointer + 1, highIndex); } } new LocalClass().quickSortRec(array, 0, array.length - 1); } static void insertionSort(int arr[]) { for (int i = 1; i < arr.length; i++) { int temp = arr[i]; int j; for (j = i - 1; j >= 0 && arr[j] > temp; j--) { arr[j + 1] = arr[j]; } arr[j + 1] = temp; } } static void countSort(int arr[]) { int max = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } } int[] countingArr = new int[max + 1]; for (int i = 0; i < arr.length; i++) { countingArr[arr[i]]++; } for (int i = 1; i <= max; i++) { countingArr[i] += countingArr[i - 1]; } int[] sortedArr = new int[arr.length + 1]; for (int i = arr.length - 1; i >= 0; i--) { sortedArr[countingArr[arr[i]] - 1] = arr[i]; countingArr[arr[i]]--; } System.arraycopy(sortedArr, 0, arr, 0, arr.length); } static void ranksSort(int[] arr){ int[] ranks = new int[arr.length]; for(int i=0; i<arr.length; i++){ int i_rank = 0; int dups = 0; for(int j=0; j<arr.length; j++){ if(arr[i]>arr[j]){ i_rank++; } else if(arr[i] == arr[j] && i!=j){ dups++; } } for(int k=0; k<=dups; k++, i_rank++){ ranks[i_rank] = arr[i]; } } System.arraycopy(ranks, 0, arr, 0, arr.length); }
Добави коментар