冒泡法

  最近总是在不断的面试,面试的过程中发觉很多的公司很注重基础方面的技术,笔试的题目都是很简单的基础题目,只要你有一定的学习能力或者工作经验,都会很好的解决,而且基本上都是跟JavaScript打交道的多!

1.用JavaScript;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//冒泡泡排序方法
 function Sort(Str) {
     var arr = Str.split("");
     var _temp;
     for (var i = 0; i < Str.length; i++) {
         for (var j = i; j < Str.length; j++) {
             if (arr[i] < arr[j]) {
                 _temp = arr[i];
                 arr[i] = arr[j];
                 arr[j] = _temp;
             }
         }
     }
     alert(arr);
 }
 Sort("1876235904");

2.自己晚上无聊,用T-SQL写的冒泡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
--冒泡泡排序方法
create procedure Sort
    @str varchar(max--传入的字符串
as
    begin
        --虚拟表,模拟数组,存放分割后的数据
        declare @table table
        (
            id int identity(1,1) primary key,
            num int
        );
        --计算器,用于循环插入字符串,默认值为1;
        declare @iCount varchar(128) = 1;
        --存放外循环中的num字段的值
        declare @temp int;
        --存放外循环中的id字段的值
        declare @id int;
        --存放内循环中的num字段的值
        declare @temp1 int;
        --存放内循环中的id字段的值
        declare @id1 int;
 
        --循环分割字符串,模拟数组
        while(@iCount <= LEN(@str))
            begin
                insert into @table(num) values(substring(@str,cast(@iCount as int),1));
                set @iCount = @iCount + 1;
            end
             
        --外层循环
        declare cursor_out cursor for select id from @table;
        open cursor_out;
         
        fetch next from cursor_out into @id;
        while @@FETCH_STATUS = 0
            begin
                --内层循环
                declare cursor_in cursor for select id from @table where id >= @id;
                open cursor_in;
                fetch next from cursor_in into @id1;
                while @@FETCH_STATUS = 0
                    begin
                        set @temp = (select num from @table where id = @id);
                        set @temp1 = (select num from @table where id = @id1);
                        if(@temp < @temp1)
                            begin
                                update @table set num = @temp where id = @id1;
                                update @table set num = @temp1 where id = @id;
                            end
                        fetch next from cursor_in into @id1;
                    end
                close cursor_in
                deallocate cursor_in
                fetch next from cursor_out into @id;
            end
 
        close cursor_out
        deallocate cursor_out
         
        select num from @table;
    end
go
    exec Sort '5231497806';
go 
    drop procedure Sort;

看来有的时候基础确实很重要,平时还需要更好的总结自己的基础!

posted @   -Xu-Zhao-  阅读(488)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥
点击右上角即可分享
微信分享提示