answersLogoWhite

0


Best Answer

#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

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

7y ago

#include<iostream>

#include<vector>

#include<functional> // for std::greater<T>

template<typename T, typename Compare=std::less<T::value_type>>

T partition (T begin, T end) {

using std::swap;

auto size {std::distance (begin, end)};

if (size<2) return begin;

auto mid {begin + size / 2};

auto last {begin + size - 1};

Compare cmp;

if (!cmp (*begin, *mid)) swap (*begin, *mid);

if (!cmp (*begin, *last)) swap (*begin, *last);

if (!cmp (*last, *mid)) swap (*last, *mid);

mid = last;

while (std::distance (begin, last)>0) {

while (std::distance (begin, last)>0 && cmp (*begin, *mid)) ++begin;

while (std::distance (begin, last)>0 && cmp (*mid, *last)) --last;

std::swap (*begin, *last);

}

std::swap (*last, *mid);

return begin;

}

template<typename T, typename Compare=std::less<T::value_type>>

void quicksort (T begin, T end) {

if (std::distance(begin, end)>1) {

T pivot = partition<T, Compare> (begin, end);

quicksort<T, Compare> (begin, pivot);

quicksort<T, Compare> (pivot+1, end);

}

}

template<typename T, typename Compare=std::less<T::value_type>>

void quicksort (T& v) {

quicksort<T::iterator, Compare> (v.begin(), v.end());

}

template<typename T>

std::ostream& operator<< (std::ostream& os, const std::vector<T>& vct){

os << "{";

for (auto val : vct) os << val << ", ";

os << "\b\b}"; // backspace over trailing comma

return os;

}

int main () {

using data_type = std::vector<int>;

data_type data {5, 6, 4, 7, 3, 8, 2, 9, 1};

std::cout << "Unsorted: " << data << std::endl;

quicksort<data_type> (data); // default sort uses std::less<int>

std::cout << " Ascend: " << data << std::endl;

quicksort<data_type, std::greater<data_type::value_type>> (data);

std::cout << " Descend: " << data << std::endl;

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Selection sort works by locating the smallest value in an unsorted array and swapping it with the first value in the array. This then splits the array in two, with sorted items to the left and unsorted to the right. The process repeats for the unsorted portion of the array until there is only one element in the unsorted portion of the array, at which point the entire array is sorted.

The following code demonstrates an implementation of the selection sort algorithm in C/C++ using an array of integers.

#include <iostream>

#include <time.h>

using namespace std;

void SelectionSort(int a[], const int Elements )

{

int i, j, m, t;

// Minimum set is 2 elements.

if(Elements<2)

return;

// Loop through all array indices except the last.

for(i=0; i<Elements-1; ++i)

{

// Assume the current index holds the smallest value.

m=i;

// Search the remainder of the array for a smaller value (store the index).

for(j=i+1; j<Elements; ++j)

if( a[j]<a[m] )

m=j;

// No need to swap values at the same index.

if(m!=i)

{

// Swap values at index i and m.

t=a[i];

a[i]=a[m];

a[m]=t;

}

}

}

int main( void )

{

const int Elements = 20;

int A[Elements];

int i;

// Seed random numbers.

srand(( unsigned int ) time( NULL ));

// Create an array with 20 two-digit numbers (range: 10-99)

for(i=0; i<Elements; ++i )

A[i]=rand()%90+10;

// Print the unsorted array.

cout << "Unsorted:\t";

for(i=0; i<Elements; ++i )

cout << A[i] << " ";

cout << endl;

// Perform a selection sort.

SelectionSort( A, Elements );

// Print the sorted array.

cout << "Sorted:\t\t";

for(i=0; i<Elements; ++i )

cout << A[i] << " ";

cout << endl;

return(0);

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a program to arrange the elements in ascending order by using selection sort technique?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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()); }


What is the connotative meaning of engineer?

skillfully or artfully arrange for (an event or situation) to occur:


How do you get fake civil engineering diploma certificate in Bangalore?

contact this person fcertificates@gmail.com he can arrange fake degree !!!

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


Which way is ascending and descending?

Ascending order is when numbers are arranged from the smallest to the largest. Descending order is when numbers are arranged from the largest to the smallest.


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

sorry


How do you order four fractions?

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


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

bit, nibble, byte, word


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.


What is the rule of order?

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


What pattern did Meneleev arrange the elements?

Meneleev arranged the elements


What is the arrange of elements call?

The elements are arranged in what is called the periodic table.


When elements are arrange in order of increasing atomic no is called?

The Periodic Table of Elements.