You are on page 1of 6

1.

Простейшая сортировка (Bubble Sort)


import java.util.Arrays;

public class HelloWorld {

private static void swap(int[] array, int ind1, int ind2) {


int tmp = array[ind1];
array[ind1] = array[ind2];
array[ind2] = tmp;
}

public static void main(String[]args) {


int[] array = { 10, 4, 11, 3, 1, 2, 5 };
System.out.println(Arrays.toString(array));
for (int left = 0; left < array.length; left++) {
int minInd = left;
for (int i = left; i < array.length; i++) {
if (array[i] < array[minInd]) {
minInd = i;
}
}
swap(array, left, minInd);
}
System.out.println(Arrays.toString(array));
}
}

2. Сортировка выбором (Selection Sort)


import java.util.Arrays;

public class HelloWorld {

private static void swap(int[] array, int ind1, int ind2) {


int tmp = array[ind1];
array[ind1] = array[ind2];
array[ind2] = tmp;
}

public static void main(String[]args) {


int[] array = { 10, 2, 10, 3, 1, 2, 5 };
System.out.println(Arrays.toString(array));
boolean needIteration = true;
while (needIteration) {
needIteration = false;
for (int i = 1; i < array.length; i++) {
if (array[i] < array[i - 1]) {
swap(array, i, i - 1);
needIteration = true;
}
}
}
System.out.println(Arrays.toString(array));
}
}

3. Сортировка вставками (Insertion Sort)


import java.util.Arrays;
public class HelloWorld {

public static void main(String[]args) {


int[] array = { 10, 2, 10, 3, 1, 2, 5 };
System.out.println(Arrays.toString(array));
for (int left = 0; left < array.length; left++) {
int value = array[left];
int i = left - 1;
for (; i >= 0; i--) {
if (value < array[i]) {
array[i + 1] = array[i];
}
else {
break;
}
}
array[i + 1] = value;
}
System.out.println(Arrays.toString(array));
}
}

4. Челночная сортировка (Shuttle Sort)


import java.util.Arrays;

public class HelloWorld {

private static void swap(int[] array, int ind1, int ind2) {


int tmp = array[ind1];
array[ind1] = array[ind2];
array[ind2] = tmp;
}

public static void main(String[]args) {


int[] array = { 10, 2, 10, 3, 1, 2, 5 };
System.out.println(Arrays.toString(array));
for (int i = 1; i < array.length; i++) {
if (array[i] < array[i - 1]) {
swap(array, i, i - 1);
for (int z = i - 1; (z - 1) >= 0; z--) {
if (array[z] < array[z - 1]) {
swap(array, z, z - 1);
}
else {
break;
}
}
}
}
System.out.println(Arrays.toString(array));
}
}

5. Сортировка Шелла
import java.util.Arrays;

public class HelloWorld {


private static void swap(int[] array, int ind1, int ind2) {
int tmp = array[ind1];
array[ind1] = array[ind2];
array[ind2] = tmp;
}

public static void main(String[]args) {


int[] array = { 10, 2, 10, 3, 1, 2, 5 };
System.out.println(Arrays.toString(array));
int gap = array.length / 2;
while (gap >= 1) {
for (int right = 0; right < array.length; right++) {
for (int c = right - gap; c >= 0; c -= gap) {
if (array[c] > array[c + gap]) {
swap(array, c, c + gap);
}
}
}
gap = gap / 2;
}
System.out.println(Arrays.toString(array));
}
}

6. Cортировка слиянием (merge sort)


import java.util.Arrays;

public class Sort {

private int[] array;

public Sort(int[] array) {


this.array = array;
}

public void mergeSort() {

int[] workSpace = new int[array.length];


recursiveMergeSort(workSpace, 0, workSpace.length - 1);
}

private void recursiveMergeSort(int[] workSpace, int lowerBound, int upperBound) {

if (lowerBound == upperBound) {
return;
}
else {
int mid = (lowerBound + upperBound) / 2;
recursiveMergeSort(workSpace, lowerBound, mid);
recursiveMergeSort(workSpace, mid + 1, upperBound);
merge(workSpace, lowerBound, mid, upperBound);
}
}

private void merge(int[] workSpace, int lowerBound, int mid, int upperBound) {

int lowBegin = lowerBound;


int lowEnd = mid;
int highBegin = mid + 1;
int highEnd = upperBound;
int j = 0;
int n = upperBound - lowerBound + 1;

while (lowBegin <= lowEnd && highBegin <= highEnd) {


if (array[lowBegin] < array[highBegin]) {
workSpace[j++] = array[lowBegin++];
}
else {
workSpace[j++] = array[highBegin++];
}
}

while (lowBegin <= lowEnd) {


workSpace[j++] = array[lowBegin++];
}

while (highBegin <= highEnd) {


workSpace[j++] = array[highBegin++];
}

for (j = 0; j < n; j++) {


array[lowerBound++] = workSpace[j];
}

public static void main(String[]args) {


int[] a = { 6,2,7,4,8,1,5,3 };

System.out.println(Arrays.toString(a));;

Sort sort = new Sort(a);


sort.mergeSort();

System.out.println(Arrays.toString(a));
}
}

6. Сортировка подсчётом (Counting Sort)


import java.util.Arrays;

public class HelloWorld {

public static void sort(int[] arr) {


int max = Arrays.stream(arr).max().getAsInt();
int min = Arrays.stream(arr).min().getAsInt();
int range = max - min + 1;
int count[] = new int[range];
int output[] = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
count[arr[i] - min]++;
}

for (int i = 1; i < count.length; i++) {


count[i] += count[i - 1];
}

for (int i = arr.length - 1; i >= 0; i--) {


output[count[arr[i] - min] - 1] = arr[i];
count[arr[i] - min]--;
}
for (int i = 0; i < arr.length; i++) {
arr[i] = output[i];
}
}

public static void main(String[]args) {


int[] array = { 10, 2, 10, 3, 1, 2, 5 };
System.out.println(Arrays.toString(array));

sort(array);
System.out.println(Arrays.toString(array));
}
}

7. Быстрая сортировка (Quick Sort)


import java.util.Arrays;

public class HelloWorld {

public static void quickSort(int[] source, int leftBorder, int rightBorder) {


int leftMarker = leftBorder;
int rightMarker = rightBorder;
int pivot = source[(leftMarker + rightMarker) / 2];
do {
while (source[leftMarker] < pivot) {
leftMarker++;
}
while (source[rightMarker] > pivot) {
rightMarker--;
}
if (leftMarker <= rightMarker) {
if (leftMarker < rightMarker) {
int tmp = source[leftMarker];
source[leftMarker] = source[rightMarker];
source[rightMarker] = tmp;
}
leftMarker++;
rightMarker--;
}
} while (leftMarker <= rightMarker);

if (leftMarker < rightBorder) {


quickSort(source, leftMarker, rightBorder);
}
if (leftBorder < rightMarker) {
quickSort(source, leftBorder, rightMarker);
}
}

public static void main(String[]args) {


int[] array = { 10, 2, 10, 3, 1, 2, 5 , 4, 6, 7, 9, 1, -1 };
System.out.println(Arrays.toString(array));

int leftBorder = 0;
int rightBorder = array.length - 1;

quickSort(array, leftBorder, rightBorder);


System.out.println(Arrays.toString(array));
}
}

You might also like