“clasificación de inserción” Código de respuesta

tipo de inserción

*** Insertion sort ***
Enter some numbers : 1 2 3 4

[1, 2, 3, 4]
Data comparison =  3
Clumsy Corncrake

tipo de inserción

#include <bits/stdc++.h>

using namespace std; 

void insertionSort(int arr[], int n)  
{  
    int i, temp, j;  
    for (i = 1; i < n; i++) 
    {  
        temp = arr[i];  
        j = i - 1;  

        while (j >= 0 && arr[j] > temp) 
        {  
            arr[j + 1] = arr[j];  
            j = j - 1;  
        }  
        arr[j + 1] = temp;  
    }  
}

int main()  
{  
    int arr[] = { 1,4,2,5,333,3,5,7777,4,4,3,22,1,4,3,666,4,6,8,999,4,3,5,32 };  
    int n = sizeof(arr) / sizeof(arr[0]);  

    insertionSort(arr, n);  

    for(int i = 0; i < n; i++){
        cout << arr[i] << " ";
    }

    return 0;  
}  
Glamorous Gibbon

tipo de inserción

 function insertionSortIterativo(array A)
     for i ← 1 to length[A] 
       do value ← A[i]
            j ← i-1
        while j >= 0 and A[j] > value 
          do A[j + 1] ← A[j]
             j ← j-1
        A[j+1] ← value;
Phil the ice cream man

clasificación de inserción

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class InsertionSorting {
    public static Scanner scanner = new Scanner(System.in);
    public static void main(String[] argh){
    int[] arrNotSorted = newArrInitilizer();
    enterValues(arrNotSorted);
    sortArray(arrNotSorted);
    print(arrNotSorted);

    }
  	//Print Array
    public static void print(int[] arr){
            System.out.print(Arrays.toString(arr));
    }
  
	/* looping from "i"(the incremented index in) ==> function
    public static int[] sortArray(int [] unsortedArr)
   	first we initilize an integer "value"= Array[from])
    this will be assigned later to the Array in the minmum value index 
    
    and while (from > 0) && (Array[from-1] > value) 
    we assign every next value to the previous one 
    
     eventually we decrement ("from")
   */
    public static void insertionSorting(int [] toBesorted, int from){
        int value = toBesorted[from];
        while(from > 0 && toBesorted[from-1] > value){
            toBesorted[from] = toBesorted[from-1];
         --from;
        }

        toBesorted[from] = value;

    }
 	
	/* Looping from index = 1, array with size one concidered sorted) 
    later "From" will be assigned to i in the function above */
    public static int[] sortArray(int [] unsortedArr){
        for(int i = 1 ; i < unsortedArr.length ; ++i){
            insertionSorting(unsortedArr,i);
        }

        return unsortedArr;
    }

  
  
    public static int[] newArrInitilizer() {
        System.out.println("Enter Array Size .");
        int arrSize = scanner.nextInt();
        int[] arr = new int[arrSize];
        return arr;
    }
		
  
  
    public static int [] enterValues(int[] arr){
        System.out.println("Array being initlized randomly with "+arr.length+" values.");
        for(int i = 0 ; i< arr.length ; ++i){
            arr[i] = (int) (Math.random()*10);
        }
        return  arr;
    }
}
Mero

tipo de inserción

 function insertionSortRicorsivo(array A, int n)
     if n>1
        insertionSortRicorsivo(A,n-1)
        value ← A[n-1]
        j ← n-2
        while j >= 0 and A[j] > value 
         do A[j + 1] ← A[j]
            j ← j-1
        A[j+1] ← value
Phil the ice cream man

Tipo de inserción

# Python program for implementation of Insertion Sort
 
# Function to do insertion sort
def insertionSort(arr):
 
    # Traverse through 1 to len(arr)
    for i in range(1, len(arr)):
 
        key = arr[i]
 
        # Move elements of arr[0..i-1], that are
        # greater than key, to one position ahead
        # of their current position
        j = i-1
        while j >= 0 and key < arr[j] :
                arr[j + 1] = arr[j]
                j -= 1
        arr[j + 1] = key
 
 
# Driver code to test above
arr = [12, 11, 13, 5, 6]
insertionSort(arr)
for i in range(len(arr)):
    print ("% d" % arr[i])
 
# This code is contributed by Mohit Kumra
Distinct Dragonfly

Respuestas similares a “clasificación de inserción”

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código