generated from Luthics/vue-elementplus-template
简单排序完成
This commit is contained in:
commit
5a062c2c50
|
@ -0,0 +1,28 @@
|
|||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
.DS_Store
|
||||
dist
|
||||
dist-ssr
|
||||
coverage
|
||||
*.local
|
||||
|
||||
/cypress/videos/
|
||||
/cypress/screenshots/
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"recommendations": ["Vue.volar", "Vue.vscode-typescript-vue-plugin"]
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
# vue-sort
|
||||
|
||||
This template should help get you started developing with Vue 3 in Vite.
|
||||
|
||||
## Recommended IDE Setup
|
||||
|
||||
[VSCode](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar) (and disable Vetur) + [TypeScript Vue Plugin (Volar)](https://marketplace.visualstudio.com/items?itemName=Vue.vscode-typescript-vue-plugin).
|
||||
|
||||
## Customize configuration
|
||||
|
||||
See [Vite Configuration Reference](https://vitejs.dev/config/).
|
||||
|
||||
## Project Setup
|
||||
|
||||
```sh
|
||||
npm install
|
||||
```
|
||||
|
||||
### Compile and Hot-Reload for Development
|
||||
|
||||
```sh
|
||||
npm run dev
|
||||
```
|
||||
|
||||
### Compile and Minify for Production
|
||||
|
||||
```sh
|
||||
npm run build
|
||||
```
|
|
@ -0,0 +1,13 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<link rel="icon" href="/favicon.ico">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Vite App</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script type="module" src="/src/main.js"></script>
|
||||
</body>
|
||||
</html>
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,25 @@
|
|||
{
|
||||
"name": "vue-sort",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"@element-plus/icons-vue": "^2.1.0",
|
||||
"@formkit/auto-animate": "^1.0.0-beta.6",
|
||||
"element-plus": "^2.3.6",
|
||||
"tsparticles": "^2.10.1",
|
||||
"vue": "^3.3.2",
|
||||
"vue-router": "^4.2.0",
|
||||
"vue3-particles": "^2.10.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vitejs/plugin-vue": "^4.2.3",
|
||||
"unplugin-auto-import": "^0.16.4",
|
||||
"unplugin-vue-components": "^0.25.1",
|
||||
"vite": "^4.3.5"
|
||||
}
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 4.2 KiB |
|
@ -0,0 +1,131 @@
|
|||
<script setup >
|
||||
import { RouterLink, RouterView } from 'vue-router'
|
||||
import Menu from './components/Menu.vue'
|
||||
|
||||
import { loadFull } from "tsparticles";
|
||||
|
||||
const particlesInit = async engine => {
|
||||
await loadFull(engine);
|
||||
};
|
||||
|
||||
const particlesLoaded = async container => {
|
||||
console.log("Particles container loaded", container);
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Particles id="tsparticles" :particlesInit="particlesInit" :particlesLoaded="particlesLoaded" :options="{
|
||||
background: {
|
||||
color: {
|
||||
value: '#0d47a1'
|
||||
}
|
||||
},
|
||||
fpsLimit: 120,
|
||||
interactivity: {
|
||||
events: {
|
||||
onClick: {
|
||||
enable: true,
|
||||
mode: 'push'
|
||||
},
|
||||
onHover: {
|
||||
enable: true,
|
||||
mode: 'repulse'
|
||||
},
|
||||
resize: true
|
||||
},
|
||||
modes: {
|
||||
bubble: {
|
||||
distance: 400,
|
||||
duration: 2,
|
||||
opacity: 0.8,
|
||||
size: 40
|
||||
},
|
||||
push: {
|
||||
quantity: 4
|
||||
},
|
||||
repulse: {
|
||||
distance: 200,
|
||||
duration: 0.4
|
||||
}
|
||||
}
|
||||
},
|
||||
particles: {
|
||||
color: {
|
||||
value: '#ffffff'
|
||||
},
|
||||
links: {
|
||||
color: '#ffffff',
|
||||
distance: 150,
|
||||
enable: true,
|
||||
opacity: 0.5,
|
||||
width: 3
|
||||
},
|
||||
collisions: {
|
||||
enable: true
|
||||
},
|
||||
move: {
|
||||
direction: 'none',
|
||||
enable: true,
|
||||
outMode: 'bounce',
|
||||
random: false,
|
||||
speed: 1,
|
||||
straight: false
|
||||
},
|
||||
number: {
|
||||
density: {
|
||||
enable: true,
|
||||
area: 800
|
||||
},
|
||||
value: 80
|
||||
},
|
||||
opacity: {
|
||||
value: 0.4
|
||||
},
|
||||
shape: {
|
||||
type: 'circle'
|
||||
},
|
||||
size: {
|
||||
random: true,
|
||||
value: 5
|
||||
}
|
||||
},
|
||||
detectRetina: true
|
||||
}" />
|
||||
<el-container>
|
||||
<el-header>
|
||||
<Menu> </Menu>
|
||||
</el-header>
|
||||
<el-main>
|
||||
<RouterView />
|
||||
</el-main>
|
||||
</el-container>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.el-container {
|
||||
height: 100vh;
|
||||
width: 100vw;
|
||||
}
|
||||
|
||||
.el-header {
|
||||
padding: 0;
|
||||
background-color: #B3C0D1;
|
||||
color: #333;
|
||||
text-align: center;
|
||||
line-height: 60px;
|
||||
}
|
||||
|
||||
.el-main {
|
||||
background-color: #E9EEF3;
|
||||
color: #333;
|
||||
text-align: center;
|
||||
line-height: 160px;
|
||||
}
|
||||
|
||||
Particles {
|
||||
position: absolute;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
z-index: -1;
|
||||
}
|
||||
</style>
|
|
@ -0,0 +1,73 @@
|
|||
/* color palette from <https://github.com/vuejs/theme> */
|
||||
:root {
|
||||
--vt-c-white: #ffffff;
|
||||
--vt-c-white-soft: #f8f8f8;
|
||||
--vt-c-white-mute: #f2f2f2;
|
||||
|
||||
--vt-c-black: #181818;
|
||||
--vt-c-black-soft: #222222;
|
||||
--vt-c-black-mute: #282828;
|
||||
|
||||
--vt-c-indigo: #2c3e50;
|
||||
|
||||
--vt-c-divider-light-1: rgba(60, 60, 60, 0.29);
|
||||
--vt-c-divider-light-2: rgba(60, 60, 60, 0.12);
|
||||
--vt-c-divider-dark-1: rgba(84, 84, 84, 0.65);
|
||||
--vt-c-divider-dark-2: rgba(84, 84, 84, 0.48);
|
||||
|
||||
--vt-c-text-light-1: var(--vt-c-indigo);
|
||||
--vt-c-text-light-2: rgba(60, 60, 60, 0.66);
|
||||
--vt-c-text-dark-1: var(--vt-c-white);
|
||||
--vt-c-text-dark-2: rgba(235, 235, 235, 0.64);
|
||||
}
|
||||
|
||||
/* semantic color variables for this project */
|
||||
:root {
|
||||
--color-background: var(--vt-c-white);
|
||||
--color-background-soft: var(--vt-c-white-soft);
|
||||
--color-background-mute: var(--vt-c-white-mute);
|
||||
|
||||
--color-border: var(--vt-c-divider-light-2);
|
||||
--color-border-hover: var(--vt-c-divider-light-1);
|
||||
|
||||
--color-heading: var(--vt-c-text-light-1);
|
||||
--color-text: var(--vt-c-text-light-1);
|
||||
|
||||
--section-gap: 160px;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--color-background: var(--vt-c-black);
|
||||
--color-background-soft: var(--vt-c-black-soft);
|
||||
--color-background-mute: var(--vt-c-black-mute);
|
||||
|
||||
--color-border: var(--vt-c-divider-dark-2);
|
||||
--color-border-hover: var(--vt-c-divider-dark-1);
|
||||
|
||||
--color-heading: var(--vt-c-text-dark-1);
|
||||
--color-text: var(--vt-c-text-dark-2);
|
||||
}
|
||||
}
|
||||
|
||||
*,
|
||||
*::before,
|
||||
*::after {
|
||||
box-sizing: border-box;
|
||||
margin: 0;
|
||||
font-weight: normal;
|
||||
}
|
||||
|
||||
body {
|
||||
min-height: 100vh;
|
||||
color: var(--color-text);
|
||||
background: var(--color-background);
|
||||
transition: color 0.5s, background-color 0.5s;
|
||||
line-height: 1.6;
|
||||
font-family: Inter, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu,
|
||||
Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
|
||||
font-size: 15px;
|
||||
text-rendering: optimizeLegibility;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 261.76 226.69"><path d="M161.096.001l-30.225 52.351L100.647.001H-.005l130.877 226.688L261.749.001z" fill="#41b883"/><path d="M161.096.001l-30.225 52.351L100.647.001H52.346l78.526 136.01L209.398.001z" fill="#34495e"/></svg>
|
After Width: | Height: | Size: 276 B |
|
@ -0,0 +1,79 @@
|
|||
body {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.ct {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
background-color: rgba(255, 255, 255, 0.5);
|
||||
/* border: 1px solid #B3C0D1; */
|
||||
border-radius: 10px;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.data {
|
||||
/* margin-left: 20px; */
|
||||
font-size: 2rem;
|
||||
color: black;
|
||||
}
|
||||
|
||||
.data-card {
|
||||
margin: 1rem;
|
||||
width: 5rem;
|
||||
height: 50px;
|
||||
display: flex;
|
||||
justify-content: flex-start;
|
||||
align-items: center;
|
||||
margin-left: 50px;
|
||||
flex-direction: row;
|
||||
}
|
||||
|
||||
.left {
|
||||
width: 70%;
|
||||
height: 100%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
.right {
|
||||
width: 30%;
|
||||
height: 100%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.el-button {
|
||||
width: 100px;
|
||||
height: 50px;
|
||||
margin: 1rem;
|
||||
}
|
||||
|
||||
.compare-1 {
|
||||
background-image: linear-gradient(to right, #b5f5ec, #d9f7be);
|
||||
}
|
||||
|
||||
.compare-2 {
|
||||
background-image: linear-gradient(to right, #f5f5b5, #f7d9be);
|
||||
}
|
||||
|
||||
.compare-3 {
|
||||
background-image: linear-gradient(to right, #f5b5b5, #f7bebe);
|
||||
}
|
||||
|
||||
.flex-grow {
|
||||
display: flex;
|
||||
flex-grow: 1;
|
||||
}
|
||||
|
||||
|
||||
.text {
|
||||
width: 90px;
|
||||
margin-right: 10px;
|
||||
font-size: 1.2rem;
|
||||
color: #999;
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<script lang="ts" setup>
|
||||
import { ref } from 'vue'
|
||||
|
||||
const activeIndex = ref('1')
|
||||
const handleSelect = (key: string, keyPath: string[]) => {
|
||||
console.log(key, keyPath)
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<el-menu :default-active="activeIndex" mode="horizontal" :ellipsis="false" @select="handleSelect" router>
|
||||
<el-menu-item index="/">排序可视化</el-menu-item>
|
||||
<el-menu-item index="bubble">冒泡排序</el-menu-item>
|
||||
<el-menu-item index="insertion">插入排序</el-menu-item>
|
||||
<el-menu-item index="selection">选择排序</el-menu-item>
|
||||
<el-menu-item index="shell">希尔排序</el-menu-item>
|
||||
<el-menu-item index="quick">快速排序</el-menu-item>
|
||||
<el-menu-item index="merge">归并排序</el-menu-item>
|
||||
<el-menu-item index="heap">堆排序</el-menu-item>
|
||||
<el-menu-item index="tournament">锦标赛排序</el-menu-item>
|
||||
<el-menu-item index="radix">基数排序</el-menu-item>
|
||||
<div class="flex-grow" />
|
||||
<el-menu-item>
|
||||
<el-link href="https://www.luthics.com/">
|
||||
跳转博客
|
||||
</el-link>
|
||||
</el-menu-item>
|
||||
</el-menu>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
|
||||
</style>
|
|
@ -0,0 +1,20 @@
|
|||
import './assets/main.css'
|
||||
|
||||
import { createApp } from 'vue'
|
||||
import App from './App.vue'
|
||||
import router from './router'
|
||||
import Particles from "vue3-particles";
|
||||
|
||||
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
|
||||
import { autoAnimatePlugin } from '@formkit/auto-animate/vue'
|
||||
|
||||
const app = createApp(App)
|
||||
app.use(autoAnimatePlugin)
|
||||
app.use(router)
|
||||
app.use(Particles)
|
||||
|
||||
app.mount('#app')
|
||||
|
||||
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
|
||||
app.component(key, component)
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import HomeView from '../views/HomeView.vue'
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(import.meta.env.BASE_URL),
|
||||
routes: [
|
||||
{
|
||||
path: '/',
|
||||
name: 'home',
|
||||
component: HomeView
|
||||
},
|
||||
{
|
||||
path: '/bubble',
|
||||
name: 'bubble',
|
||||
component: () => import('../views/BubbleView.vue')
|
||||
},
|
||||
{
|
||||
path: '/insertion',
|
||||
name: 'insertion',
|
||||
component: () => import('../views/InsertionView.vue')
|
||||
},
|
||||
{
|
||||
path: '/selection',
|
||||
name: 'selection',
|
||||
component: () => import('../views/SelectionView.vue')
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
export default router
|
|
@ -0,0 +1,108 @@
|
|||
<script setup>
|
||||
import { ref } from 'vue'
|
||||
|
||||
const datas = ref([])
|
||||
const sorting = ref(false)
|
||||
let currentIndex1 = ref(-1)
|
||||
let currentIndex2 = ref(-1)
|
||||
|
||||
const bubbleSort = async (desc) => {
|
||||
if (sorting.value) {
|
||||
return
|
||||
}
|
||||
sorting.value = true
|
||||
// 实现冒泡排序
|
||||
|
||||
// 1. 获取数据
|
||||
const data = datas.value
|
||||
|
||||
// 2. 排序
|
||||
// 2.1 外层循环,控制比较的轮数
|
||||
for (let i = 0; i < data.length - 1; i++) {
|
||||
// 2.2 内层循环,控制每轮比较的次数
|
||||
for (let j = 0; j < data.length - 1 - i; j++) {
|
||||
// 检查是否停止排序
|
||||
if (!sorting.value) {
|
||||
return
|
||||
}
|
||||
// 2.3 比较相邻的两个数,如果前面的数大于后面的数,则交换位置
|
||||
currentIndex1.value = j
|
||||
currentIndex2.value = j + 1
|
||||
await new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve()
|
||||
}, 400)
|
||||
})
|
||||
// 根据 desc 判断是升序还是降序
|
||||
if (desc === 0) {
|
||||
if (data[j] > data[j + 1]) {
|
||||
const temp = data[j]
|
||||
data[j] = data[j + 1]
|
||||
data[j + 1] = temp
|
||||
}
|
||||
} else {
|
||||
if (data[j] < data[j + 1]) {
|
||||
const temp = data[j]
|
||||
data[j] = data[j + 1]
|
||||
data[j + 1] = temp
|
||||
}
|
||||
}
|
||||
// 每次延时
|
||||
await new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve()
|
||||
}, 200)
|
||||
})
|
||||
}
|
||||
}
|
||||
currentIndex1.value = -1
|
||||
currentIndex2.value = -1
|
||||
sorting.value = false
|
||||
}
|
||||
|
||||
const shuffle = () => {
|
||||
// 范围从 20~99
|
||||
datas.value = []
|
||||
for (let i = 0; i < 10; i++) {
|
||||
datas.value.push(Math.floor(Math.random() * 80 + 20))
|
||||
}
|
||||
}
|
||||
|
||||
const stop = () => {
|
||||
sorting.value = false
|
||||
}
|
||||
|
||||
const compare = (index) => {
|
||||
// 判断当前索引是否与指针重合
|
||||
if (index === currentIndex1.value) {
|
||||
return 'compare-1'
|
||||
}
|
||||
if (index === currentIndex2.value) {
|
||||
return 'compare-2'
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
shuffle()
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<el-row class="ct">
|
||||
<!-- 两个div,左右各占一半 -->
|
||||
<div class="left" v-auto-animate>
|
||||
<el-card v-for="(data, index) in datas" :key="data" :class="['data-card', compare(index)]"
|
||||
:style="'width:' + data / 2 + 'rem;'">
|
||||
<div class="data">{{ data }}</div>
|
||||
</el-card>
|
||||
</div>
|
||||
<div class="right">
|
||||
<el-button type="primary" @click="bubbleSort(0)" :disabled="sorting">从小到大</el-button>
|
||||
<el-button type="primary" @click="bubbleSort(1)" :disabled="sorting">从大到小</el-button>
|
||||
<el-button type="primary" @click="shuffle" :disabled="sorting">随机打乱</el-button>
|
||||
<el-button type="primary" @click="stop" :disabled="!sorting">停止排序</el-button>
|
||||
</div>
|
||||
</el-row>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
</style>
|
|
@ -0,0 +1,28 @@
|
|||
<script setup lang="ts">
|
||||
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<el-row class="ct">
|
||||
<el-text class="bt" z-index="999" style="position: absolute;">
|
||||
请从导航栏中选择你想查看的项目
|
||||
</el-text>
|
||||
</el-row>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.bt {
|
||||
font-size: 3rem;
|
||||
color: black;
|
||||
}
|
||||
|
||||
.ct {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
background-color: rgba(255, 255, 255, 0.5);
|
||||
/* border: 1px solid #B3C0D1; */
|
||||
border-radius: 10px;
|
||||
height: 100%;
|
||||
}
|
||||
</style>
|
|
@ -0,0 +1,107 @@
|
|||
<script setup>
|
||||
import { ref } from 'vue'
|
||||
|
||||
const datas = ref([])
|
||||
const sorting = ref(false)
|
||||
let currentIndex1 = ref(-1)
|
||||
let currentIndex2 = ref(-1)
|
||||
|
||||
const insertionSort = async (desc) => {
|
||||
if (sorting.value) {
|
||||
return
|
||||
}
|
||||
sorting.value = true
|
||||
// 实现插入排序
|
||||
|
||||
// 1. 获取数据
|
||||
const data = datas.value
|
||||
|
||||
// 2. 排序
|
||||
// 2.1 外层循环,控制待插入的元素位置
|
||||
for (let i = 1; i < data.length; i++) {
|
||||
// 2.2 将待插入元素存入 temp 变量中
|
||||
const temp = data[i]
|
||||
currentIndex1.value = i
|
||||
// 2.3 内层循环,查找插入位置
|
||||
let j = i - 1
|
||||
while (j >= 0 && ((desc === 0 && data[j] > temp) || (desc === 1 && data[j] < temp))) {
|
||||
// 检查是否停止排序
|
||||
if (!sorting.value) {
|
||||
return
|
||||
}
|
||||
currentIndex2.value = j
|
||||
// 2.4 暂存当前比较元素
|
||||
await new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve()
|
||||
}, 400)
|
||||
})
|
||||
// 2.5 将比待插入元素大的元素后移
|
||||
data[j + 1] = data[j]
|
||||
currentIndex2.value = j+1
|
||||
j--
|
||||
}
|
||||
await new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve()
|
||||
}, 400)
|
||||
})
|
||||
// 2.6 将待插入元素插入到正确位置
|
||||
data[j + 1] = temp
|
||||
currentIndex1.value = j + 1
|
||||
|
||||
}
|
||||
currentIndex1.value = -1
|
||||
currentIndex2.value = -1
|
||||
sorting.value = false
|
||||
}
|
||||
|
||||
const shuffle = () => {
|
||||
// 范围从 20~99
|
||||
datas.value = []
|
||||
for (let i = 0; i < 10; i++) {
|
||||
datas.value.push(Math.floor(Math.random() * 80 + 20))
|
||||
}
|
||||
}
|
||||
|
||||
const stop = () => {
|
||||
currentIndex1.value = -1
|
||||
currentIndex2.value = -1
|
||||
sorting.value = false
|
||||
}
|
||||
|
||||
const compare = (index) => {
|
||||
// 判断当前索引是否与指针重合
|
||||
if (index === currentIndex1.value) {
|
||||
return 'compare-1'
|
||||
}
|
||||
if (index === currentIndex2.value) {
|
||||
return 'compare-2'
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
shuffle()
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<el-row class="ct">
|
||||
<!-- 两个div,左右各占一半 -->
|
||||
<div class="left" v-auto-animate>
|
||||
<el-card v-for="(data, index) in datas" :key="data" :class="['data-card', compare(index)]"
|
||||
:style="'width:' + data / 2 + 'rem;'">
|
||||
<div class="data">{{ data }}</div>
|
||||
</el-card>
|
||||
</div>
|
||||
<div class="right">
|
||||
<el-button type="primary" @click="insertionSort(0)" :disabled="sorting">从小到大</el-button>
|
||||
<el-button type="primary" @click="insertionSort(1)" :disabled="sorting">从大到小</el-button>
|
||||
<el-button type="primary" @click="shuffle" :disabled="sorting">随机打乱</el-button>
|
||||
<el-button type="primary" @click="stop" :disabled="!sorting">停止排序</el-button>
|
||||
</div>
|
||||
</el-row>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
|
||||
</style>
|
|
@ -0,0 +1,143 @@
|
|||
<script setup>
|
||||
import { ref } from 'vue'
|
||||
|
||||
const datas = ref([])
|
||||
const sorting = ref(false)
|
||||
let currentIndex1 = ref(-1)
|
||||
let currentIndex2 = ref(-1)
|
||||
let currentIndex3 = ref(-1)
|
||||
const desc = ref(0)
|
||||
|
||||
const asc_texts = ['', '最小值', '当前值', '选中值']
|
||||
const desc_texts = ['', '最大值', '当前值', '选中值']
|
||||
|
||||
const selectionSort = async (descc) => {
|
||||
if (sorting.value) {
|
||||
return
|
||||
}
|
||||
sorting.value = true
|
||||
desc.value = descc
|
||||
// 实现选择排序
|
||||
|
||||
// 1. 获取数据
|
||||
const data = datas.value
|
||||
|
||||
// 2. 排序
|
||||
// 2.1 外层循环,控制比较的轮数
|
||||
for (let i = 0; i < data.length - 1; i++) {
|
||||
let minIndex = i
|
||||
currentIndex3.value = i
|
||||
// 2.2 内层循环,找到最小值的 index
|
||||
for (let j = i + 1; j < data.length; j++) {
|
||||
// 检查是否停止排序
|
||||
if (!sorting.value) {
|
||||
return
|
||||
}
|
||||
// 根据 desc 判断是升序还是降序
|
||||
if (desc.value === 0) {
|
||||
if (data[j] < data[minIndex]) {
|
||||
minIndex = j
|
||||
}
|
||||
} else {
|
||||
if (data[j] > data[minIndex]) {
|
||||
minIndex = j
|
||||
}
|
||||
}
|
||||
currentIndex1.value = minIndex
|
||||
currentIndex2.value = j
|
||||
// 每次延时
|
||||
await new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve()
|
||||
}, 200)
|
||||
})
|
||||
}
|
||||
// 2.3 把找到的最小值放到当前轮的最左侧
|
||||
const temp = data[i]
|
||||
data[i] = data[minIndex]
|
||||
data[minIndex] = temp
|
||||
}
|
||||
|
||||
currentIndex1.value = -1
|
||||
currentIndex2.value = -1
|
||||
currentIndex3.value = -1
|
||||
sorting.value = false
|
||||
}
|
||||
|
||||
const shuffle = () => {
|
||||
// 范围从 20~99
|
||||
datas.value = []
|
||||
for (let i = 0; i < 10; i++) {
|
||||
datas.value.push(Math.floor(Math.random() * 80 + 20))
|
||||
}
|
||||
}
|
||||
|
||||
const stop = () => {
|
||||
sorting.value = false
|
||||
}
|
||||
|
||||
const compare = (index) => {
|
||||
// 判断当前索引是否与指针重合
|
||||
if (index === currentIndex1.value) {
|
||||
return 'compare-1'
|
||||
}
|
||||
if (index === currentIndex2.value) {
|
||||
return 'compare-2'
|
||||
}
|
||||
if (index === currentIndex3.value) {
|
||||
return 'compare-3'
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
// 传入全数据列表索引,返回对应的文字
|
||||
const getText = (index) => {
|
||||
if (index === currentIndex1.value) {
|
||||
if (desc.value === 0) {
|
||||
return asc_texts[1]
|
||||
} else {
|
||||
return desc_texts[1]
|
||||
}
|
||||
}
|
||||
if (index === currentIndex2.value) {
|
||||
if (desc.value === 0) {
|
||||
return asc_texts[2]
|
||||
} else {
|
||||
return desc_texts[2]
|
||||
}
|
||||
}
|
||||
if (index === currentIndex3.value) {
|
||||
if (desc.value === 0) {
|
||||
return asc_texts[3]
|
||||
} else {
|
||||
return desc_texts[3]
|
||||
}
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
shuffle()
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<el-row class="ct">
|
||||
<!-- 两个div,左右各占一半 -->
|
||||
<div class="left" v-auto-animate>
|
||||
<el-card v-for="(data, index) in datas" :key="data" :class="['data-card', compare(index)]"
|
||||
:style="'width:' + data / 1.4 + 'rem;'">
|
||||
<el-row>
|
||||
<div class="text">{{ getText(index,) }}</div>
|
||||
<div class="data">{{ data }}</div>
|
||||
</el-row>
|
||||
</el-card>
|
||||
</div>
|
||||
<div class="right">
|
||||
<el-button type="primary" @click="selectionSort(0)" :disabled="sorting">从小到大</el-button>
|
||||
<el-button type="primary" @click="selectionSort(1)" :disabled="sorting">从大到小</el-button>
|
||||
<el-button type="primary" @click="shuffle" :disabled="sorting">随机打乱</el-button>
|
||||
<el-button type="primary" @click="stop" :disabled="!sorting">停止排序</el-button>
|
||||
</div>
|
||||
</el-row>
|
||||
</template>
|
||||
|
||||
<style scoped></style>
|
|
@ -0,0 +1,25 @@
|
|||
import { fileURLToPath, URL } from 'node:url'
|
||||
|
||||
import { defineConfig } from 'vite'
|
||||
import vue from '@vitejs/plugin-vue'
|
||||
import AutoImport from 'unplugin-auto-import/vite'
|
||||
import Components from 'unplugin-vue-components/vite'
|
||||
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
|
||||
|
||||
// https://vitejs.dev/config/
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
vue(),
|
||||
AutoImport({
|
||||
resolvers: [ElementPlusResolver()],
|
||||
}),
|
||||
Components({
|
||||
resolvers: [ElementPlusResolver()],
|
||||
}),
|
||||
],
|
||||
resolve: {
|
||||
alias: {
|
||||
'@': fileURLToPath(new URL('./src', import.meta.url))
|
||||
}
|
||||
}
|
||||
})
|
Loading…
Reference in New Issue