We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Insertion Sort - Part 2
Insertion Sort - Part 2
+ 0 comments Python Insertion Simple Solution
for i in range(1,n): key=arr[i] j=i-1 while j>=0 and key<arr[j]: arr[j+1]=arr[j] j-=1 arr[j+1]=key print(*arr)
+ 0 comments Python:
def insertionSort2(n, arr): for i in range(1, n): j = i-1 while j >= 0: if arr[j+1] < arr[j]: temp = arr[j+1] arr[j+1] = arr[j] arr[j] = temp j -= 1 print(*arr)
+ 0 comments C++ solution
void insertionSort2(int n, vector arr) {
for(int i=1; i<n; i++){ for(int j=i-1; j>=0;j--){ if(arr[j+1]<arr[j]){ int x = arr[j]; arr[j] = arr[j+1]; arr[j+1] = x; } } for(int l = 0; l<n ; l++){ cout<<arr[l]<<" "; } cout<<endl; }
}
+ 0 comments Swift
func insertionSort2(n: Int, arr: [Int]) -> Void { var sortArr: [Int] = arr for i in 1..<sortArr.count { for j in stride(from: i, through: 0, by: -1) { if arr[i] < sortArr[j] { sortArr[j+1] = sortArr[j] sortArr[j] = arr[i] } else { continue } } printer(sortArr) } } func printer(_ arr: [Int]) { print(arr.map({ String($0)}).joined(separator: " ")) }
+ 1 comment JavaScript Solution
function insertionSort2(n, arr) { // Write your code here for(let i=0; i<n-1; i++){ // From here, This block of code performs Insertion Sort-1 if(arr[i+1] < arr[i]){ var val = arr[i+1], j = i; while(arr[j] > val){ arr[j+1] = arr[j]; j--; } arr[j+1] = val; } // End of insertion sort - 1 console.log(...arr) }
}
Load more conversations
Sort 498 Discussions, By:
Please Login in order to post a comment