Sorting Algorithms
1. Bubble sort.
public static int bubble3(int[] arr) {
boolean swapped;
int count = 0;
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
count++;
if (arr[j] > arr[j + 1]) {
swapped = true;
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
if (!swapped) break;
}
return count;
}
2. Selection sort.
public static int selectionSort1(int[] arr) {
int count = 0;
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
count++;
if (arr[j] < arr[min]) {
min = j;
}
}
if (min != i) {
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
return count;
}
improved version:
public static int selectionSort2(int[] arr) {
int count = 0;
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i; j < n; j++) {
count++;
if (arr[j] < arr[min]) {
min = j;
}
}
if (min != i) {
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
} else {
break;
}
}
return count;
}
3. Insertion sort
public static void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int temp = arr[i];
int j = i;
while (j > 0 && arr[j - 1] > temp) {
arr[j] = arr[j - 1];
j--;
}
arr[j] = temp;
}
}
public static void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j > 0; j--) {
if (arr[j] < arr[j - 1]) {
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
} else {
break;
}
}
}
}
4. Counting sort:
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
using namespace std;
void countingSortPositiveArray(int a[], int n) {
int max = INT_MIN;
int min = INT_MAX;
int range;
// step 1: find max and min values for defining range
for (int i = 0; i < n; i++) {
if (a[i] > max) {
max = a[i];
}
if (a[i] < min) {
min = a[i];
}
}
range = max - min + 1;
// step 2: initialize count array
int count[range];
for (int i = 0; i < range; i++) {
count[i] = 0;
}
for (int i = 0; i < n; i++) {
count[a[i]]++;
}
// step 3: update count array
for (int i = 1; i <= range; i++) {
count[i] = count[i] + count[i - 1];
}
// step 4: initialize result array
int b[n];
for (int i = n - 1; i >= 0; i--) {
b[--count[a[i]]] = a[i];
}
for (int i = 0; i < n; i++) {
cout << b[i] << " ";
}
}
void countingSortNegativeArray(int a[], int n) {
int max = INT_MIN;
int min = INT_MAX;
int range;
// step 1: find max and min values for defining range
for (int i = 0; i < n; i++) {
if (a[i] > max) {
max = a[i];
}
if (a[i] < min) {
min = a[i];
}
}
range = max - min + 1;
// step 2: initialize count array
int count[range];
for (int i = 0; i < range; i++) {
count[i] = 0;
}
// offset each element
for (int i = 0; i < n; i++) {
count[a[i] - min]++;
}
// step 3: update count array
for (int i = 1; i <= range; i++) {
count[i] = count[i] + count[i - 1];
}
// step 4: initialize result array
int b[n];
for (int i = n - 1; i >= 0; i--) {
b[--count[a[i] - min]] = a[i];
}
for (int i = 0; i < n; i++) {
cout << b[i] << " ";
}
}
int main() {
int arr[] = {2, 1, 1, 0, 2, 5, 4, 0, 2, 8, 7, 7, 9, 2, 0, 1, 9};
countingSortPositiveArray(arr, 17);
cout << endl;
int arr2[] = {3, 7, 0, -3, 9, 7, 0, 1, 4};
countingSortNegativeArray(arr2, 9);
}
Комментарии
Отправить комментарий