feat: 手写冒泡排序
parent
8ba78f6f6d
commit
9a401f2821
@ -0,0 +1,30 @@
|
||||
<template>
|
||||
<div>请查看页面源码</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
/**
|
||||
* 第一题 1. 冒泡排序
|
||||
*/
|
||||
function useBubbleSort() {
|
||||
const arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
|
||||
const bubbleSort = (arr: number[]): number[] => {
|
||||
const arrLength = arr.length - 1;
|
||||
|
||||
for (let i = 0; i < arrLength; i++) {
|
||||
for (let j = 0; j < arrLength - i; j++) {
|
||||
let temp = arr[j + 1];
|
||||
if (arr[j] > arr[j + 1]) {
|
||||
arr[j + 1] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log(arr);
|
||||
return arr;
|
||||
};
|
||||
|
||||
bubbleSort(arr);
|
||||
}
|
||||
useBubbleSort();
|
||||
</script>
|
||||
Loading…
Reference in New Issue