Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/Sorts/InsertionSort.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract InsertionSort{

function sort(uint[] memory data) public pure returns(uint[] memory) {

insertionSort(data, data.length );

return data ;
}

/* Insertion sort is a sorting algorithm that places an unsorted
* element at its suitable place in each iteration. */

function insertionSort( uint [] memory _inputData, uint _size) public pure {
for (uint i = 1; i < _size; ++i) {
uint key = uint (_inputData[i]);
int256 _j = int256 (i - 1);
uint j = i - 1;

/* Move elements of _inputData[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (_j >= 0 && _inputData[j] > key) {
_inputData[j + 1] = _inputData[j];
_j = _j - 1;
if(_j!=-1){
j= uint256 (_j);
}
}
_j=_j+1;
j= uint256 (_j);
_inputData[j] = key;
}
}
}