answersLogoWhite

0

#include<iostream>

#include<vector>

template<class T>

void insertion_sort (std::vector<T>& v)

{

if (v.size()<2U)

return;

for (size_t index=1; index<v.size(); ++index)

{

T value = v[index];

size_t gap = index;

size_t prev = index-1;

while (gap && value<v[left] )

v[gap--]=v[left--];

v[gap] = value;

}

}

int main()

{

std::vector<int> vect {42, 1, 27, 8, 15, 4};

for (auto v : vect) std::cout << v << '\t';

std::cout << std::endl;

insertion_sort (vect);

for (auto v : vect) std::cout << v << '\t';

std::cout << std::endl;

}

User Avatar

Wiki User

10y ago

What else can I help you with?

Continue Learning about Engineering

Java program to arrange any numbers in ascending order using scanner classes?

To arrange numbers in ascending order using Java, you can utilize the Scanner class to read input from the user and then sort the numbers using an array. Here's a simple example: import java.util.Arrays; import java.util.Scanner; public class AscendingOrder { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(&quot;Enter the number of elements: &quot;); int n = scanner.nextInt(); int[] numbers = new int[n]; System.out.println(&quot;Enter the numbers:&quot;); for (int i = 0; i &lt; n; i++) { numbers[i] = scanner.nextInt(); } Arrays.sort(numbers); System.out.println(&quot;Numbers in ascending order: &quot; + Arrays.toString(numbers)); scanner.close(); } } This program collects a specified number of integers from the user, sorts them using Arrays.sort(), and then displays the sorted list.


Algorithm to arrange three numbers in ascending order using else if ladder?

step 1:start step 2:input the values of a,b,c step 3:if(a&gt;b&amp;&amp;a&gt;c) max=a step 4:else if(b&gt;c) max=b step 5:else max=c step 6:output:max step 7:stop


Write a program that will arrange the elements of a 10-integer array in ascending and descending order?

import java.io.*; public class descendingorder { public static void main(String args[]) throws IOException { BufferedReader in= new BufferedReader(new InputStreamReader(System.in)); int i,j,n,t; int a[]=new int[20]; System.out.println("Enter the number of elements:"); n=Integer.parseInt(in.readLine()); System.out.println("Enter the elements of the array:"); for(i=0;i&lt;n;i++) { a[i]=Integer.parseInt(in.readLine()); } for(i=0;i&lt;n;i++) { for(j=0;j&lt;n;j++) { if(a[i]&gt;a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; ] } } System.out.println("The orderer lit..."); { for(i=0;i&lt;n;i++) { System.out.println(a[i]); } } }


Write a program in java to enter 3 digits or more arrange the digits of the entered number in ascending order and display the result?

public static void main(String[] args) { int val = 100; int val1 = 50; System.out.println("Number of digits in " + val + " is: " + new String(val + "").length()); System.out.println("Number of digits in " + val1 + " is: " + new String(val1 + "").length()); }


Sorting given names in assending order?

To sort given names in ascending order, you can use various methods, such as programming languages or spreadsheet software. A common approach is to use the built-in sorting functions, which typically arrange the names alphabetically from A to Z. Ensure that you account for any capitalization differences, as some sorting algorithms may treat uppercase and lowercase letters differently. Once sorted, the names will be listed in a clear and organized manner.

Related Questions

Draw a flowchart to arrange 3 numbers in ascending order?

draw a flow chart to arrange 3 numbers in ascending order


How do you arrange words in cline and examples?

its arranged by ascending and descending order.........


How do you arrange 4 numbers in ascending order without array?

addends


How can the keyword "sorting" be implemented in pseudo code to arrange the elements of an array a of integers in ascending order?

To implement the keyword &quot;sorting&quot; in pseudo code to arrange the elements of an array a of integers in ascending order, you can use the following algorithm: Start by iterating through the array a from the first element to the second-to-last element. Compare each element with the next element in the array. If the current element is greater than the next element, swap their positions. Continue this process until the entire array is sorted in ascending order. Here is a simple example of pseudo code for implementing the sorting algorithm: for i from 0 to length(a) - 1 do for j from 0 to length(a) - i - 1 do if aj aj 1 then swap(aj, aj 1) end if end for end for This pseudo code represents a basic implementation of a sorting algorithm to arrange the elements of an array in ascending order.


What will be the program to arrange numbers stored in array in ascending order using pointers?

sorry


What is the process to find the median of an array of numbers?

To find the median of an array of numbers, first, arrange the numbers in ascending order. If the array has an odd number of elements, the median is the middle number. If the array has an even number of elements, the median is the average of the two middle numbers.


How can you arrange bit byte word nibble in ascending order?

bit, nibble, byte, word


How do you order four fractions?

Find the lowest common denominator, convert them, and arrange them in ascending order.


Simple c program for selection sort?

The Selection Sort definition is rather simple : find the largest number (element) in a list and move it to it's position in sorted form.You can perform selection sort like, smallest elements are in the beginning and largest element at the end.Now how this element arrange to it's exact position,We can do this by swapping elements at highest index and the process is continue till all the elements are sorted.


Which way is ascending and descending?

Ascending means increasing in value or moving higher, while descending means decreasing in value or moving lower. In a numeric sequence, ascending would go from lowest to highest, while descending would go from highest to lowest.


What is the rule of order?

the rule defines whether we have to arrange in ascending order or the decreasing order as per demand


How do you arrange the numbers 7 9 56 and 72?

To arrange the numbers 7, 9, 56, and 72 in ascending order, you would start by comparing the numbers from left to right. The smallest number is 7, followed by 9, then 56, and finally 72. So, the numbers arranged in ascending order would be 7, 9, 56, and 72.