使用T-SQL模仿程序实现冒泡排序
来源:互联网 发布:得无异乎的互 编辑:程序博客网 时间:2024/05/18 09:03
/*
Project: 此次研究的课题是模仿程序实现冒泡排序(数据库只需要排序)
*/
--1.数据库实现冒泡排序(只需要按照要求升序或倒序排列即可)
--2.数据库模仿程序实现冒泡排序
--a.申明表
--a.1 临时数据表
declare @tbl table(
ID int identity(1,1) primary key not null,
Num int not null
)
--b.插入临时数据
insert into @tbl(Num)
select 30 as num union all
select 5 as num union all
select 20 as num union all
select 1 as num union all
select 16 as num union all
select 7 as num
select * from @tbl
--c.实现冒泡排序过程
declare @cnt int ,@index int
select @cnt = count(0) from @tbl
set @index = 1
while(@index <=@cnt) begin --d.外层控制循环的次数(按照升序)
declare @index_2 int ,@cnt_2 int,@pre_Num int , @pre_ID int,
@cur_Num int , @cur_ID int
set @index_2 =1
select @cnt_2 = count(0) from @tbl
while(@index_2 <= @cnt_2) begin --e.控制顺序
select @cur_Num = Num,@cur_ID = ID from @tbl where ID = @index_2
if(@index_2 = 1) begin
set @pre_ID = @index_2
set @pre_Num = @cur_Num
end else begin
--f.交换彼此的位置
if(@cur_Num < @pre_Num) begin
update @tbl set Num = @pre_Num
where ID = @index_2
update @tbl set Num = @cur_Num
where ID = @pre_ID
set @pre_ID = @index_2
end
else begin
set @pre_Num = @cur_Num
set @pre_ID = @index_2
set @index_2 +=1
continue
end
end
set @index_2 +=1
end
set @index +=1
end
select * from @tbl
0 0
- 使用T-SQL模仿程序实现冒泡排序
- T-SQL实现数据结构中的冒泡算法和快速排序
- 冒泡排序程序实现
- T-SQL单向冒泡排序算法
- 使用冒泡排序模仿Qsort函数对不同数据排序
- 模仿qsort的功能实现一个通用的冒泡排序。
- 使用单循环实现冒泡排序
- 使用C++实现冒泡排序
- asp 实现的冒泡排序程序
- java实现冒泡排序算法程序
- Java程序实现冒泡排序、直接排序、插入排序方法
- 【C】模仿 qsort 的功能实现一个通用的冒泡排序
- C语言数组实现冒泡排序和选择排序程序
- 选择排序和冒泡排序小结与程序实现
- C语言数组实现冒泡排序和选择排序程序
- C++ 使用模板实现冒泡排序
- 如何使用汇编语言实现冒泡排序?
- 如何使用C语言实现冒泡排序
- Delphi应用程序的调试(1-10)
- rvm 安装问题
- java记——随机抽牌(加强版)
- RNN(三) 在SLU中的应用
- Spark修炼之道(基础篇)——Linux大数据开发基础:第十三节:Shell编程入门(五)
- 使用T-SQL模仿程序实现冒泡排序
- android UI界面设计(1)
- 主席树
- ListFragment的使用
- IM6Q yocto平台 uboot v2014.04_3.14.28_1.0.0_ga编译和调试
- 【BLE】CC2541之indicate
- QT5 学习之路23---事件总结
- UVa 1225 - Digit Counting【打表】
- LinkedList 增删真的比ArrayList快吗?