Selection Sort - Javascript

Shared by: nuur

javascript

1
async function selectionSortWithDelay(arr) {
2
  const len = arr.length;
3

4
  for (let i = 0; i < len - 1; i++) {
5
    let minIndex = i;
6

7
    for (let j = i + 1; j < len; j++) {
8
      if (arr[j] < arr[minIndex]) {
9
        minIndex = j;
10
      }
11
    }
12

13
    if (minIndex !== i) {
14
      const temp = arr[i];
15
      arr[i] = arr[minIndex];
16
      arr[minIndex] = temp;
17

18
      // console.log(arr)
19
      const transition = document.startViewTransition(() => renderChart());
20

21
      await delay(speed); // Delay in milliseconds
22
    }
23
  }
24

25
  return arr;
26
}