Answers for "when to use insertion sort"

7

insertion sort java

Insertion program
public class InsertionSortExample
{
   public void sort(int[] arrNum)
   {
      int number = arrNum.length;
      for(int a = 1; a < number; ++a)
      {
         int keyValue = arrNum[a];
         int b = a - 1;
         while(b >= 0 && arrNum[b] > keyValue)
         {
            arrNum[b + 1] = arrNum[b];
            b = b - 1;
         }
         arrNum[b + 1] = keyValue;
      }
   }
   static void displayArray(int[] arrNum)
   {
      int num = arrNum.length;
      for(int a = 0; a < num; ++a)
      {
         System.out.print(arrNum[a] + " ");
      }
      System.out.println();
   }
   public static void main(String[] args)
   {
      int[] arrInput = { 50, 80, 10, 30, 90, 60 };
      InsertionSortExample obj = new InsertionSortExample();
      obj.sort(arrInput);
      displayArray(arrInput);
   }
}
Posted by: Guest on October-23-2020
2

insertion sort

//I Love Java
import java.util.*;
import java.io.*;
import static java.util.stream.Collectors.toList;
import java.util.stream.*;

public class Insertion_Sort_P {
    public static void main(String[] args) throws IOException {
        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));

        List<Integer> arr = Stream.of(buffer.readLine().replaceAll("\\s+$", " ").split(" ")).map(Integer::parseInt)
                .collect(toList());

        insertion_sort(arr);

        System.out.println(arr);
    }

    public static void insertion_sort(List<Integer> arr) {
        for (int i = 1; i <= arr.size() - 1; i++) {
            steps(arr, i);
        }
    }

    public static void steps(List<Integer> arr, int comp) {
        for (int i = 0; i <= comp - 1; i++) {
            if (arr.get(comp) < arr.get(i)) {
                swap(arr, i, comp);
            }
        }
    }

    static void swap(List<Integer> arr, int i, int j) {
        int temp = arr.get(i);
        arr.set(i, arr.get(j));
        arr.set(j, temp);
    }
}
Posted by: Guest on June-12-2021
1

insertion sort

def insertionSort(arr): 
    for i in range(1, len(arr)): 
        key = arr[i] 
        j = i-1
        while j >= 0 and key < arr[j] : 
                arr[j + 1] = arr[j] 
                j -= 1
        arr[j + 1] = key
Posted by: Guest on October-07-2020
2

insertion sort

// Por ter uma complexidade alta,
// não é recomendado para um conjunto de dados muito grande.
// Complexidade: O(n²) / O(n**2) / O(n^2)
// @see https://www.youtube.com/watch?v=TZRWRjq2CAg
// @see https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html

function insertionSort(vetor) {
    let current;
    for (let i = 1; i < vetor.length; i += 1) {
        let j = i - 1;
        current = vetor[i];
        while (j >= 0 && current < vetor[j]) {
            vetor[j + 1] = vetor[j];
            j--;
        }
        vetor[j + 1] = current;
    }
    return vetor;
}

insertionSort([1, 2, 5, 8, 3, 4])
Posted by: Guest on May-29-2020
10

insertion sort

#insertion sort
def insert(arr):
    for i in range(1,len(arr)):
        while arr[i-1] > arr[i] and i > 0:
            arr[i], arr[i-1] = arr[i-1], arr[i]
            i -= 1 
    return arr 
arr = [23, 55, 12, 99, 66, 33]
print(insert(arr))
Posted by: Guest on July-24-2021
0

insertion sort

//insertion sort
#include <iostream>

using namespace std;
void insertion_sort(int arr[],int n)
{
    int value,index;
    for(int i=1;i<n;i++)
    {
        value=arr[i];
        index=i;
        while(index>0&&arr[index-1]>value)
        {
            arr[index]=arr[index-1];
            index--;

        }
        arr[index]=value;
    }
}
void display(int arr[],int n)
{
    for(int i=0;i<n;i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}

int main()
{
    int n;
    cout<<"enter the size of the array:"<<endl;
    cin>>n;
    int array_of_numbers[n];
    cout<<"enter the elements of the array:"<<endl;
    for(int i=0;i<n;i++)
    {
        cin>>array_of_numbers[i];
    }
    cout<<"array before sorting:"<<endl;
    display(array_of_numbers,n);
    insertion_sort(array_of_numbers,n);
    cout<<"array after sorting is:"<<endl;
    display(array_of_numbers,n);

    return 0;
}
Posted by: Guest on May-25-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language