InsertionSort – A simple routine with minimal overhead

InsertionSort – A simple routine with minimal overhead

' InsertionSort.  A simple routine with minimal overhead.  Should never be used ' to sort long lists because of its O(N^2) behavior,'  but is the method of choice for sorting short (5-50 key) lists or long lists ' that have been mostly sorted by a faster algorithm.  InsertionSort is faster ' than either Bubble or SelectionSort and should be used anywhere you would ' consider using those.  Sorts in place (no extra memory needed) and is stable ' (preserves the original order of records with equal keys).  Works by creating ' a sorted list at the beginning of the array of keys.  As each unsorted key to ' the right is examined, it is compared back thru the sorted list until the ' right position to insert it is found.  Two versions are given.  pInsertS is ' an indirect (pointerized) version for strings,'  which can be adapted to doubles by changing the declaration of A().  InsertL ' is a direct version for longs, which can be adapted to integers.' ' Speed:  Abysmally slow for anything but short lists.'' Bottom line:  should be used only to finish up for faster sorts with higher ' overhead; for that purpose, this is the sort to choose.' Usage:  Dim S1(L To R) As StringDim P1(L To R) As LongDim L1(L To R) As Long For I = L To R    S1(I) = GetRandomString()    P1(I) = I    L1(I) = GetRandomLong()Next IpInsertS L, R, S1, P1InsertL L, R, L1' CODE:Sub pInsertS(L As Long, R As Long, A() As String, P() As Long)    Dim LP As Long    Dim RP As Long    Dim TMP As Long    Dim T As String    'RP points to the first unsorted key.       For RP = L + 1 To R     'Get the new value.       TMP = P(RP)        T = A(TMP)    'Compare it back thru the sorted part as long as it's bigger.        For LP = RP To L + 1 Step -1            If T < A(P(LP - 1)) Then P(LP) = P(LP - 1) Else Exit For        Next LP    'It's bigger than all keys to the left, so insert it here.        P(LP) = TMP    Next RPEnd SubSub InsertL(L As Long, R As Long, A() As Long)    Dim LP As Long    Dim RP As Long    Dim TMP As Long        For RP = L + 1 To R        TMP = A(RP)        For LP = RP To L + 1 Step -1            If TMP < A(LP - 1) Then A(LP) = A(LP - 1) Else Exit For        Next LP        A(LP) = TMP    Next RPEnd Sub

Share the Post:
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

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as