Занятие 7. Справочник
Занятие 7. Справочник
Функция сортировки по неубыванию выбором
static void selectSort(int[]a) { int N = a.length; for (int i = 0; i< N - 1; i++) { int min = i; for (int j = i + 1; j < N; j++) { if (a[j] < a[min]) { min = j; } } int t = a[min]; a[min] = a[i]; a[i] = t; } }
Функция сортировки по неубыванию обменами с оптимизацией
static void bubbleSort(int[] a) { int N = a.length; int count = 1; //чтобы войти в цикл первый раз for (int i = 0; i < N - 1 && count > 0; i++)//счетчик проходов { count = 0; for (int j = 0; j < N - 1 - i; j++) //проход по массиву { if (a[j] > a[j + 1]) { int t = a[j]; //обмен в случае a[j] = a[j + 1];//необходимости a[j + 1] = t; count++; } } } }
Встроенные функции сортировки класса Arrays
static void sort(int[] a)Sorts the specified array of ints into ascending numerical order.
static void sort(int[] a, int fromIndex, int toIndex)Sorts the specified range of the specified array of ints into ascending numerical order