帮我用vue写一个冒泡排序
时间: 2023-03-29 12:01:55 浏览: 101
好的,以下是用 Vue 实现冒泡排序的代码:
<template>
<div>
<h2>冒泡排序</h2>
<div>
<label>请输入要排序的数字,以逗号分隔:</label>
<input v-model="inputStr" />
<button @click="sort">排序</button>
</div>
<div>
<label>排序结果:</label>
<span v-for="(num, index) in nums" :key="index">{{ num }} </span>
</div>
</div>
</template>
<script>
export default {
data() {
return {
inputStr: "",
nums: [],
};
},
methods: {
sort() {
let arr = this.inputStr.split(",");
for (let i = ; i < arr.length - 1; i++) {
for (let j = ; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
this.nums = arr;
},
},
};
</script>
阅读全文