冒泡法

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

1.用JavaScript;

       //冒泡泡排序方法
        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写的冒泡

--冒泡泡排序方法
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 @ 2010-07-11 18:21  -Xu-Zhao-  阅读(486)  评论(0编辑  收藏  举报