Improve on the Bubble Sort

Improve on the Bubble Sort

A bubble sort’s execution time is a multiple of the square of the number of elements. Because of this, the bubble sort is said to be an n-squared algorithm. You can easily make improvements to a bubble sort to speed it up.

One way is to reverse the direction of passes reading the array, instead of always reading the array in the same direction. This makes out-of-place elements travel quickly to their correct position. This version of a bubble sort is called the shaker sort, because it imparts a shaking motion to the array:

 Public Sub Shaker(Item() As Variant)	Dim Exchange As Boolean	Dim Temp As Variant	Dim x As Integer	Do		Exchange = False		For x = (UBound(Item)) To (LBound(Item) + 1) Step -1			If Item(x - 1) > Item(x) Then				Temp = Item(x - 1)				Item(x - 1) = Item(x)				Item(x) = Temp				Exchange = True			End If		Next x		For x = (LBound(Item) + 1) To (UBound(Item))			If Item(x - 1) > Item(x) Then				Temp = Item(x - 1)				Item(x - 1) = Item(x)				Item(x) = Temp				Exchange = True			End If		Next x	Loop While ExchangeEnd Sub

Although the shaker sort improves the bubble sort, it still executes as an n-squared algorithm. However, because most programmers can code a bubble sort with their eyes closed, this is a nice way to shave 25 to 33 percent off the required execution time without having to dig out the algorithm books. Still, you don’t want to use either a bubble or shaker sort for extremely large data sets.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes