T-SQL single-way bubble sort algorithm

xiaoxiao2021-03-06  55

--2004-09-16 - GROUNDSEL - One-way bubbling algorithm: from top to down Create Procedure sp_bubble_sort @ID varchar (8000) - Separate ascreate table #order (ID Int Id Id Id Int Intity (1, 1) VALUE INT)

Declare @i smallint, @LEN Smallint, @charindex smallint, @ value1 smallint, @ value2 smallint, @flag bit

SELECT @i = 0 Select @LEN = LEN (@ID)

- turn string to temporary table while @i <@Len Begin select @charindex = charIndex (',', @ id) if @charindex <> 0 Begin Insert Into #order (Value) Select Left (@ id, @ Charindex - 1) SELECT @ID = Right (@ id, len (@ID) - @charindex) SELECT @i = @i @charindex end else begin insert @Order (value) select @ID select @i = @LEN End end

- @ FLAG logo Sort by completing set @flag = 1

- The number of cycles is sorted by sorting while isnull (@ flag, 0) = 1 begin set @i = 1 set @flag = 0 set @ value1 = null set @ value2 = null declare mycur cost for select value from # # # # # @Eut Order Open MyCur Fetch Next From MyCur Into @ Value1 While @@ fetch_Status = 0 Begin Select @ Value2 = Value From #Order Where ID = @I 1 If @ Value1> @ Value2 Begin Set @Flag = 1 Update #Order Set Value = @ Value2 Where ID = @I Update #Order Set Value = @ Value1 Where ID = @I 1 End Select @I = @I 1 Select @ Value2 = Null Fetch Next From MyCur Into @ Value1 End Close MyCur Deallocate MyCur End Set @I = 0 set @ID = ''

SELECT @ID = @id cast (Value As Varchar (20)) ',' from #order

SELECT LEFT (@ ID, len (@ID) - 1) GoExecute sp_bubble_sort '23, 6, 2, 12, 19, -6 '----------------- 6, 2 , 6, 12, 19, 23

转载请注明原文地址:https://www.9cbs.com/read-116260.html

New Post(0)