SelectionSort

 

 

Java

public class Ersatz {
    public static void main(String[] args) {
        int[] ints = new int[8];
        for (int v = 0; v < ints.length; ++v) {
            ints[v] = (int) (Math.random() * 9) + 1;
        }
        System.out.println(Arrays.toString(ints));
        selectionSort(ints);
        System.out.println(Arrays.toString(ints));
    }

    private static void selectionSort(int[] array) {
        /*
        length
        0           length - 1
        1           length - 2
        length -2   1
         */
        int v = 0;
        for (int i = 1; i < array.length; i++) {
            v += i;
        }
        System.out.println("v = " + v);
        int count_iter = 0, count_swap = 0, minIndex;
        for (int i = 0; i < array.length - 1; i++) {
            minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                count_iter++;
                if (array[minIndex] > array[j]) {
                    minIndex = j;
                }
            }
            if (minIndex != i) {
                count_swap++;
                array[minIndex] = array[minIndex] ^ array[i];
                array[i] = array[minIndex] ^ array[i];
                array[minIndex] = array[minIndex] ^ array[i];
            }
        }
        System.out.println("count_iter = " + count_iter);
        System.out.println("count_swap = " + count_swap);
    }
}

 

reversed order

public class Ersatz {
    public static void main(String[] args) {
        int[] ints = new int[8];
        for (int v = 0; v < ints.length; ++v) {
            ints[v] = (int) (Math.random() * 9) + 1;
        }
        System.out.println(Arrays.toString(ints));
        selectionSort(ints);
        System.out.println(Arrays.toString(ints));
    }

    private static void selectionSort(int[] array) {
        /*
        length
        0           length - 1
        1           length - 2
        length -2   1
         */
        int v = 0;
        for (int i = 1; i < array.length; i++) {
            v += i;
        }
        System.out.println("v = " + v);
        int count_iter = 0, count_swap = 0, maxIndex;
        for (int i = array.length - 1; i > 0; --i) {
            maxIndex = i;
            for (int j = i - 1; j >= 0; --j) {
                count_iter++;
                if (array[maxIndex] < array[j]) {
                    maxIndex = j;
                }
            }
            if (maxIndex != i) {
                count_swap++;
                array[maxIndex] = array[maxIndex] ^ array[i];
                array[i] = array[maxIndex] ^ array[i];
                array[maxIndex] = array[maxIndex] ^ array[i];
            }
        }
        System.out.println("count_iter = " + count_iter);
        System.out.println("count_swap = " + count_swap);
    }
}

  

Enhanced

public class Ersatz {
    public static void main(String[] args) {
        int[] ints = new int[8];
        for (int v = 0; v < ints.length; ++v) {
            ints[v] = (int) (Math.random() * 9) + 1;
        }
        ints = new int[]{1, 8, 9, 5, 1, 4, 6, 5};
        System.out.println(Arrays.toString(ints));
        selectionSort(ints);
        System.out.println(Arrays.toString(ints));
    }

    private static void selectionSort(int[] array) {
        int iterations = 0, swaps = 0, minIndex, maxIndex, orignalMaxIndex;
        for (int i = 0; i < array.length / 2; ++i) {
            minIndex = i;
            maxIndex = array.length - 1 - i;
            orignalMaxIndex = maxIndex;
            for (int j = i + 1; j <= array.length - 1 - i; ++j) {
                iterations++;
                if (array[minIndex] > array[j]) {
                    minIndex = j;
                }
                if (array[maxIndex] < array[array.length - 1 - j]) {
                    maxIndex = array.length - 1 - j;
                }
            }
            if (minIndex != i) {
                swaps++;
                array[minIndex] = array[minIndex] ^ array[i];
                array[i] = array[minIndex] ^ array[i];
                array[minIndex] = array[minIndex] ^ array[i];
                if (maxIndex == i) {  // 如果maxIndex == i,而且发生交换(minIndex != i),则maxIndex位置的元素被交换到了minIndex处
                    maxIndex = minIndex;
                }
            }
            if (maxIndex != orignalMaxIndex) {
                swaps++;
                array[maxIndex] = array[maxIndex] ^ array[orignalMaxIndex];
                array[orignalMaxIndex] = array[maxIndex] ^ array[orignalMaxIndex];
                array[maxIndex] = array[maxIndex] ^ array[orignalMaxIndex];
            }
            System.out.println("minIndex = " + minIndex);
            System.out.println("maxIndex = " + maxIndex);
            System.out.println(Arrays.toString(array));
        }
        System.out.println("count_iter = " + iterations);
        System.out.println("count_swap = " + swaps);
    }
}

 

Python

import random

m = [x + 1 for x in range(9)]
random.shuffle(m)

awe = [
    m,
    [x for x in range(1, 10)],
    [x - 1 for x in range(10, 1, -1)],
    [1 for x in range(9)],
    [2, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]

print(*awe, sep='\n')

for item in awe:
    iterations = 0
    swaps = 0
    length = len(item)
    for i in range(length // 2):
        min_index = i
        max_index = -i - 1
        max_origin = max_index

        for j in range(i + 1, length - i):
            iterations += 1
            if item[min_index] > item[j]:
                min_index = j
            if item[max_index] < item[-j - 1]:
                max_index = -j - 1

        # maximum and minimum equal, so all elements are equal
        if item[min_index] == item[max_index]:
            break

        if min_index != i:
            swaps += 1
            item[min_index], item[i] = item[i], item[min_index]
            if max_index == i - length:  # negative index, so max_index == i - length
                max_index = min_index - length
        if max_index != max_origin:
            swaps += 1
            item[max_index], item[max_origin] = item[max_origin], item[max_index]

print(*awe, sep='\n')

 

posted @ 2023-11-18 15:42  ascertain  阅读(5)  评论(0编辑  收藏  举报