Sorting Vba Bubble Insertion Quick

download Sorting Vba Bubble Insertion Quick

of 6

Transcript of Sorting Vba Bubble Insertion Quick

  • 8/12/2019 Sorting Vba Bubble Insertion Quick

    1/6

    3 Sorting Algorithms in Excel VBA: Bubble Sort, Insertion Sort, Quick Sort

    1 Nyasha Madavo, VBA Developer.net 3 ways to sort in VBA

    Introduction

    Sorting algorithms have important applications inValue at Riskcalculations and other risk and pricing functions in finance. Forexample, when calculatingVaRviaMonte Carlo simulation,we run asimulation of the asset prices many times, and save the change inportfolio value for each simulation. We then need to sort the manythousands of values in order to pick theValue at Riskat the requiredconfidence interval.

    Bubble Sort Algorithm and VBA Code

    The bubble sort is a simple sorting algorithm that compares adjacentpairs of values within an array, adjusting the order of the values toplace the smaller value before the larger one. It does this repeatedlyfor a cycle through the entire array. The algorithm counts whether ornot any changes have been made during a cycle. The algorithm stopswhen no changes have been made.

    The bubble sort is a relatively slow and non-scalable algorithm that isnot recommended for use in larger data sets.

    Function Bubblesort(Array_Values As Object)Application.Calculation = xlCalculationManualApplication.Volatile (False)

    Application.ScreenUpdating = False

    Dim size As Long, changed As LongDim tmp As Double, nums() As DoubleDim limit As Long, i As Long, cycle As Longlimit = Array_Values.Rows.Count

    ReDim Preserve nums(1 To limit)

    For i = 1 To limitnums(i) = Array_Values(i)

    Next i

    timer_val = Timer

    Docycle = cycle + 1changed = 0

    For i = 1 To limit - 1

    If nums(i) > nums(i + 1) Thentmp = nums(i)nums(i) = nums(i + 1)nums(i + 1) = tmpchanged = 1End If

    Next i

    Loop Until changed = 0Bubblesort = WorksheetFunction.Transpose(nums)

    Application.Volatile (True)

    Application.Calculation = xlCalculationAutomaticApplication.ScreenUpdating = True

    End Function

    http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/
  • 8/12/2019 Sorting Vba Bubble Insertion Quick

    2/6

  • 8/12/2019 Sorting Vba Bubble Insertion Quick

    3/6

    3 Sorting Algorithms in Excel VBA: Bubble Sort, Insertion Sort, Quick Sort

    3 Nyasha Madavo, VBA Developer.net 3 ways to sort in VBA

    Dim i As Long, j As Long

    Dim less_or_equal() As DoubleDim greater() As DoubleDim Array_Values() As DoubleDim New_Array_Values()Dim pivot As DoubleDim array1, array2

    limit = UBound(Param_Array)

    ReDim Preserve Array_Values(1 To limit)

    For i = 1 To limit

    Array_Values(i) = Param_Array(i)Next i

    Select Case limit

    Case Is 2pivot = WorksheetFunction.RoundUp(limit / 2, 0)pivot = WorksheetFunction.Median(Array_Values(pivot),

    Array_Values(1), Array_Values(limit))

    End Select

    Dim limit As Long, lim_less_or_equal As Long, lim_greater As Long

    lim_less_or_equal = 0lim_greater = 0

    For i = 1 To limitIf Array_Values(i)

  • 8/12/2019 Sorting Vba Bubble Insertion Quick

    4/6

    3 Sorting Algorithms in Excel VBA: Bubble Sort, Insertion Sort, Quick Sort

    4 Nyasha Madavo, VBA Developer.net 3 ways to sort in VBA

    ReDim Preserve array1(1 To limit1)ReDim Preserve array2(1 To limit2)

    For i = 1 To limit1array1(i) = Param_Array1(i)

    Next i

    For i = 1 To limit2array2(i) = Param_Array2(i)Next i

    ReDim Preserve new_array(1 To limit1 + limit2)

    For i = 1 To limit1new_array(i) = array1(i)

    Next i

    j = 0For i = (limit1 + 1) To (limit1 + limit2)

    j = j + 1new_array(i) = array2(j)

    Next i

    Concatenate_VBA = new_array

    End Function

    HowVBA Developer.netCan Save You Time and

    money

    You can get complete Excel apps fromVBA Developer.netcontainingthe code in this document, customisation,VBA developmentof anyExcel, Access and Outlook apps, as well as C# and C++ add-ins andtechnical documentation.

    Visit VBA Developer.net

    Examples ofVBAdocuments fromVBADeveloper.net

    How to build a Black Scholes VBA Option Pricer

    How to build a Black Scholes C# Option Pricer

    How to build a Black Scholes VBA Option Pricer for FXOptions

    How to build a Black Scholes VBA Option Pricer for EquityOptions

    How to build a Black Scholes VBA Option Pricer using MonteCarlo Simulation

    http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.info/http://vbadeveloper.info/http://vbadeveloper.info/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/optionpricerblackscholesvba.pdfhttp://vbadeveloper.net/optionpricerblackscholesvba.pdfhttp://vbadeveloper.net/optionpricerblackscholescsharp.pdfhttp://vbadeveloper.net/optionpricerblackscholesvbafx.pdfhttp://vbadeveloper.net/optionpricerblackscholesvbafx.pdfhttp://vbadeveloper.net/blackscholesvbaoptionpricermontecarlosimulation.pdfhttp://vbadeveloper.net/blackscholesvbaoptionpricermontecarlosimulation.pdfhttp://vbadeveloper.net/blackscholesvbafxoptionpricermontecarlosimulation.pdfhttp://vbadeveloper.net/blackscholesvbafxoptionpricermontecarlosimulation.pdfhttp://vbadeveloper.net/blackscholesvbafxoptionpricermontecarlosimulation.pdfhttp://vbadeveloper.net/blackscholesvbafxoptionpricermontecarlosimulation.pdfhttp://vbadeveloper.net/blackscholesvbaoptionpricermontecarlosimulation.pdfhttp://vbadeveloper.net/blackscholesvbaoptionpricermontecarlosimulation.pdfhttp://vbadeveloper.net/optionpricerblackscholesvbafx.pdfhttp://vbadeveloper.net/optionpricerblackscholesvbafx.pdfhttp://vbadeveloper.net/optionpricerblackscholescsharp.pdfhttp://vbadeveloper.net/optionpricerblackscholesvba.pdfhttp://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.info/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/http://vbadeveloper.net/
  • 8/12/2019 Sorting Vba Bubble Insertion Quick

    5/6

    3 Sorting Algorithms in Excel VBA: Bubble Sort, Insertion Sort, Quick Sort

    5 Nyasha Madavo, VBA Developer.net 3 ways to sort in VBA

    How to build a Black Scholes VBA Option Pricer for Binary

    Options

    How to build a Black Scholes VBA Option Pricer for EquityBarrier Options

    How to build a Black Scholes VBA Option Pricer for ExoticAsian Options

    How to build a Black Scholes VBA Option Pricer for ExoticLookback Options

    How to build an Equity Option Pricer using the Binomial Treein Excel VBA

    How to code a Choleskey Decomposition in VBA (NumericalMethods for Excel)

    3 ways to sort in VBA

    How to Code a Multivariate Value at Risk (VaR) VBA MonteCarlo Simulation

    How To Code the Newton-Raphson Method in Excel VBA

    How to Model Volatility Smiles, Volatility Term Structure andthe Volatility Surface in Excel VBA

    How To Write Use Cases for a Portfolio Reporting VBA Tool

    How To Design Classes for Object Orientated VBAProgramming

    How To Write a User Interface Model For a Portfolio ReportingVBA Tool

    How To Create a Semantic Object Model For a PortfolioReporting VBA Tool

    How To Normalise a Database For VBA Apps

    How To Create a Database using SQL Scripts for a PortfolioReporting VBA App

    How to Write Stored Procedures in SQL/Access/VBA for aPortfolio Reporting VBA App

    How to Use Cursors in SQL for a Portfolio Reporting VBATool

    How to Move Data from Access to Excel with SQL for aPortfolio Reporting VBA App

    Portfolio Reporting VBA Tool: Inserting Data into SQL/AccessDatabases from Excel

    http://vbadeveloper.net/blackscholesvbafxbinaryoptionpricermontecarlosimulation.pdfhttp://vbadeveloper.net/blackscholesvbafxbinaryoptionpricermontecarlosimulation.pdfhttp://vbadeveloper.net/blackscholesvbabarrieroptionpricermontecarlosimulation.pdfhttp://vbadeveloper.net/blackscholesvbabarrieroptionpricermontecarlosimulation.pdfhttp://vbadeveloper.net/blackscholesvbaasianoptionspricer.pdfhttp://vbadeveloper.net/blackscholesvbaasianoptionspricer.pdfhttp://vbadeveloper.net/optionpricerblackscholesvbalookback.pdfhttp://vbadeveloper.net/optionpricerblackscholesvbalookback.pdfhttp://vbadeveloper.net/optionpricerblackscholesvbabinomialtree.pdfhttp://vbadeveloper.net/optionpricerblackscholesvbabinomialtree.pdfhttp://vbadeveloper.net/numericalmethodsvbacholeskydecomposition.pdfhttp://vbadeveloper.net/numericalmethodsvbacholeskydecomposition.pdfhttp://vbadeveloper.net/sortingvbabubbleinsertionquick.pdfhttp://vbadeveloper.net/valueatriskmultivariatevbamontecarlo.pdfhttp://vbadeveloper.net/valueatriskmultivariatevbamontecarlo.pdfhttp://vbadeveloper.net/newtonraphsonvba.pdfhttp://vbadeveloper.net/volatilitysmilestermstructuresurface.pdfhttp://vbadeveloper.net/volatilitysmilestermstructuresurface.pdfhttp://vbadeveloper.net/portfolioreportingusecasesvba.pdfhttp://vbadeveloper.net/vbaclassesdesign.pdfhttp://vbadeveloper.net/vbaclassesdesign.pdfhttp://vbadeveloper.net/portfolioreportinguserinterfacevba.pdfhttp://vbadeveloper.net/portfolioreportinguserinterfacevba.pdfhttp://vbadeveloper.net/portfolioreportingsemanticobjectvba.pdfhttp://vbadeveloper.net/portfolioreportingsemanticobjectvba.pdfhttp://vbadeveloper.net/portfolioreportingdatabasenormalisationvba.pdfhttp://vbadeveloper.net/portfolioreportingdatabasesqlscriptsvba.pdfhttp://vbadeveloper.net/portfolioreportingdatabasesqlscriptsvba.pdfhttp://vbadeveloper.net/portfolioreportingstoredproceduresvba.pdfhttp://vbadeveloper.net/portfolioreportingstoredproceduresvba.pdfhttp://vbadeveloper.net/portfolioreportingsqlcursorsvba.pdfhttp://vbadeveloper.net/portfolioreportingsqlcursorsvba.pdfhttp://vbadeveloper.net/portfolioreportingaccesstoexcelvbasql.pdfhttp://vbadeveloper.net/portfolioreportingaccesstoexcelvbasql.pdfhttp://vbadeveloper.net/portfolioreportingexceltoaccessvbasql.pdfhttp://vbadeveloper.net/portfolioreportingexceltoaccessvbasql.pdfhttp://vbadeveloper.net/portfolioreportingexceltoaccessvbasql.pdfhttp://vbadeveloper.net/portfolioreportingexceltoaccessvbasql.pdfhttp://vbadeveloper.net/portfolioreportingaccesstoexcelvbasql.pdfhttp://vbadeveloper.net/portfolioreportingaccesstoexcelvbasql.pdfhttp://vbadeveloper.net/portfolioreportingsqlcursorsvba.pdfhttp://vbadeveloper.net/portfolioreportingsqlcursorsvba.pdfhttp://vbadeveloper.net/portfolioreportingstoredproceduresvba.pdfhttp://vbadeveloper.net/portfolioreportingstoredproceduresvba.pdfhttp://vbadeveloper.net/portfolioreportingdatabasesqlscriptsvba.pdfhttp://vbadeveloper.net/portfolioreportingdatabasesqlscriptsvba.pdfhttp://vbadeveloper.net/portfolioreportingdatabasenormalisationvba.pdfhttp://vbadeveloper.net/portfolioreportingsemanticobjectvba.pdfhttp://vbadeveloper.net/portfolioreportingsemanticobjectvba.pdfhttp://vbadeveloper.net/portfolioreportinguserinterfacevba.pdfhttp://vbadeveloper.net/portfolioreportinguserinterfacevba.pdfhttp://vbadeveloper.net/vbaclassesdesign.pdfhttp://vbadeveloper.net/vbaclassesdesign.pdfhttp://vbadeveloper.net/portfolioreportingusecasesvba.pdfhttp://vbadeveloper.net/volatilitysmilestermstructuresurface.pdfhttp://vbadeveloper.net/volatilitysmilestermstructuresurface.pdfhttp://vbadeveloper.net/newtonraphsonvba.pdfhttp://vbadeveloper.net/valueatriskmultivariatevbamontecarlo.pdfhttp://vbadeveloper.net/valueatriskmultivariatevbamontecarlo.pdfhttp://vbadeveloper.net/sortingvbabubbleinsertionquick.pdfhttp://vbadeveloper.net/numericalmethodsvbacholeskydecomposition.pdfhttp://vbadeveloper.net/numericalmethodsvbacholeskydecomposition.pdfhttp://vbadeveloper.net/optionpricerblackscholesvbabinomialtree.pdfhttp://vbadeveloper.net/optionpricerblackscholesvbabinomialtree.pdfhttp://vbadeveloper.net/optionpricerblackscholesvbalookback.pdfhttp://vbadeveloper.net/optionpricerblackscholesvbalookback.pdfhttp://vbadeveloper.net/blackscholesvbaasianoptionspricer.pdfhttp://vbadeveloper.net/blackscholesvbaasianoptionspricer.pdfhttp://vbadeveloper.net/blackscholesvbabarrieroptionpricermontecarlosimulation.pdfhttp://vbadeveloper.net/blackscholesvbabarrieroptionpricermontecarlosimulation.pdfhttp://vbadeveloper.net/blackscholesvbafxbinaryoptionpricermontecarlosimulation.pdfhttp://vbadeveloper.net/blackscholesvbafxbinaryoptionpricermontecarlosimulation.pdf
  • 8/12/2019 Sorting Vba Bubble Insertion Quick

    6/6

    3 Sorting Algorithms in Excel VBA: Bubble Sort, Insertion Sort, Quick Sort

    6 Nyasha Madavo, VBA Developer.net 3 ways to sort in VBA

    Portfolio Reporting VBA Tool: Connecting Excel with SQL &Access Databases

    How To Design Classes For an Option Pricer in VBA: UMLConcepts

    http://vbadeveloper.net/portfolioreportingexceltosqlvba.pdfhttp://vbadeveloper.net/portfolioreportingexceltosqlvba.pdfhttp://vbadeveloper.net/optionpricerclassesdesignvba.pdfhttp://vbadeveloper.net/optionpricerclassesdesignvba.pdfhttp://vbadeveloper.net/optionpricerclassesdesignvba.pdfhttp://vbadeveloper.net/optionpricerclassesdesignvba.pdfhttp://vbadeveloper.net/portfolioreportingexceltosqlvba.pdfhttp://vbadeveloper.net/portfolioreportingexceltosqlvba.pdf