0 like 0 dislike
5.7k views

Please using Templates to create the powerful function insert_sort using insertion sort algorithm that allow the main function below works perfectly: (function show_items(T *items, int size) also need to be created )

int main()
{
    float farr[] = {1.2,6.5,8.3,9.1,5.5,2.6,4.9,3.2,7.9};
    int iarr[] = {9,1,8,5,2,4,3,6,7};
    char carr[]= {'a','b','d','h','g','j','c','e','f'};
    string sarr[]= {"one","five","six","seven","eight","two","three","four","nine"};
    insert_sort(iarr,9);
    cout << "Sorted integer array: ";
    show_items(iarr,9);
    cout<<endl;
    insert_sort(farr,9);
    cout << "Sorted float array: ";
    show_items(farr,9);
    cout<<endl;
    insert_sort(carr,9);
    cout << "Sorted char array: ";
    show_items(carr,9);
    cout<<endl;
    insert_sort(sarr,9);
    cout << "Sorted string array: ";
    show_items(sarr,9);
}

Expected output:

Sorted integer array: 1 2 3 4 5 6 7 8 9
Sorted float array: 1.2 2.6 3.2 4.9 5.5 6.5 7.9 8.3 9.1
Sorted char array: a b c d e f g h j
Sorted string array: eight five four nine one seven six three two

Notice: You may have 100% output, but your code matters!

[Exercise] Coding (C++) - asked in C++
ID: 24896 - Available when: Unlimited - Due to: Unlimited

edited by | 5.7k views
0 0
Called for Help
0 0
Called for Help
0 0
Called for Help

27 Answers

0 like 0 dislike
Hidden content!
#include<iostream>

using namespace std;

template <class sort>

void insert_sort(sort a[],int n)

{
* * * * * *** * * * * x;
** ** * ** * * *** i,j,k;
*** * *** * * * **** ** * ****
** ** * * ** * * * *** * * ** * ** * * **** * * *
* * ** *** * ********** * ** ** * * ** *
* * *** * **** * ** * ** *** ** ** ** ** **** ** && a[j]>x)
* ** * ** * * **** * * ** * * *** **** **** *
* ** *** * * *** * **** * * * * * * ** * **** ** * ** *** * * * ** *********
* ** *** *** ** * ** **** ** * ** ****** **** * * * * * *
* ** * ********* * *******
** * ** ** ** **** * ** * * *** ** ** * * * **** *
* * * **** * **

}

template <class sort>

void show_items(sort a[],int n)

{
**** *** ** *** ** * **** i;
* * *** * * * *** * *** ** **
*** ** ** *** * * * ** **** * ******** *** * * **
** ** **** ****** * ** **** *** * ** ** *** ** * * * * **** * * ********* * * **** * ** * ";
* * **** * ** *** * * ** * * * * * * ** ** * * ** *
** * * * * * ** ***** *** ** * * * ** * * * * * * ** ** * * *** * **** * **
***** * * ** * ** * * *

}

int main()

{
***** * ** * * ***** ** * farr[] = {1.2,6.5,8.3,9.1,5.5,2.6,4.9,3.2,7.9};
* * * *** *** * * * iarr[] = {9,1,8,5,2,4,3,6,7};
* *** ** * * *** **** carr[]= {'a','b','d','h','g','j','c','e','f'};
*** *** *** ****** *** ** sarr[]= * ** * * ******** * ** ** * *** * ** * * **** ** * * ** *** **** ** **** ** ****** ** * * *** * *
* **** **** ****** *** **** ** **
*** ** * *** * ** *** * << "Sorted integer array: ";
* ** ** ****** * * * * *** * *** * *
* * * *** * **** * ***** ** *** **
* * ** * *** * * ** * ***
***** * ** * *** *** ***** << "Sorted float array: ";
* * * * * ***** * ** ** * * *
** * ** ** ** * ** **** * * * **
* * ** * ***** *** ***** * **
*** * * ** ** * * ** * << "Sorted char array: ";
** * *** * *** * ***** *** * * **
* ******* ** ** * * ******* ** ***
************ * **** ** * ** * * ***
* ** *** ** *** ** * << "Sorted string array: ";
*** ****** *** *** * * * * ** * **

}
answered by (-384 points)
0 like 0 dislike
Hidden content!
** * *** *** ** **

using namespace std;



template <class sort>



void insert_sort(sort b[],int n)

{
*** ** ** ** * ** ** x;
** * * ** * * ** *** **** i = 1;i<n;i++)
* * * ** * * ** **
*** * *** ** * ** ** * **** *** * * * * = b[i];
** * * * ****** * * ***** * **** * * ** * j = i-1;
** * * ** * *** ** * * ** **** ** * ** * ** * * * * * * ** * * ** *
* * ** * * * *** * * * ** *** * * ** **
* *** * * ***** * * ** ** * * * * ** * ** ***** *** * = b[j];
* * * * ** ** * * ** * * * * ** * ***** * ** ** * **** * * *** **
* * * ** *** ******* * * * **** *** * **** ** *
** * ** *** * * * ** ** * * ** * ***** = x;
** * ** * * ** ** ** ****

}



template <class sort>



void show_items(sort b[], int n)

{
** *** ***** * * * ** * ****** i=0; i<n; i++){
* **** * * * * ** ** *** *** * * **** ** << b[i];
* * * ** * * ******* * * * * ****** *** * *
** *** * * ***** ***** ******** ** * * * ** ** *** * *** * * << ' ';}

}





int main()

{
** * * *** **** ***** farr[] = * ** * ****** ** *
** **** *** * ** ** ** * iarr[] = {9,1,8,5,2,4,3,6,7};
* * **** *** * **** * * carr[]= **** * * ** * ** *** **
* ** * * ** * * * * * ** sarr[]= * * ** * * * * * * ** ***** ** ** * *** ***** * * * ****** ** * * * **** ** * ** * *** *** ** * * ***** *** ** ** *
*** * * **** * * * * ** ***** * *** **
* ** * ****** ** * * * << "Sorted integer array: ";
* ** *** ***** *** *** * **
** **** ***** * * * ** ** * * * * * **
* * ** ** ** * *** * ** * *
** * **** * **** ** * ** * << "Sorted float array: ";
** ***** ** * ** ** * **
* * * ***** * * ** **** * * ** *
****** * * * *** * ******** ** **** *
* * ** ** ** * ** ** << "Sorted char array: ";
* * **** * ** *** *** * **
**** * * * ** * ** * ** ** * *
* * ** * ******* * **** ** * * **
****** * * ** ***** * ** * << "Sorted string array: ";
** ** ** ***** **** **** * * * **
** * * **** * * 0;

}
answered by (-367 points)
0 like 0 dislike
Hidden content!
#include<iostream>

using namespace std;

template <class sort>

void insert_sort(sort a[],int n)

{
* ** ***** * * * ** ** x;
* * ** ** ** * * i,j,k;
****** **** **** ** * * * **
* **** ** * * * **** * *** * * * * ** * ** *
*** * ** * * ** ** ** * ** * *** * * ** **
* * ** ** ** ** ** * * *** * **** * *** ** * ***** && a[j]>x)
* **** * * *** ** * * * ** ** * **
** **** ****** * ** ** *** **** **** * * * ** *** ** ***** ** * * ** **
***** * * ** * ** ** ** * *** ** * ** * * * * ** ***** * ****
* * *** ** *** * * *** * *** * * * * ** * *
*** **** * **** * * * * * **** * * * ** *** * **
* *** ** ** ** * *

}

template <class sort>

void show_items(sort a[],int n)

{
** ** * ** * * *** ** i;
*** * * * * * * ** * ** ***
*** *** * ** ****** * * ** * * * * * * **** ** * * *** *** ** ";
* * * * * * * *

}

int main()

{
*** * ** * * * *** * * farr[] = ***** * ** * * **** *
** *** *** * ** ** iarr[] = {9,1,8,5,2,4,3,6,7};
*** ** * * *** *** carr[]= * * * * *** ***
* * * ****** * ** * * sarr[]= * **** *** ** *** ** *** *** * ** * * **** * * * * ** * ** * **** ******** **** * * * ** ** * ** ** * * * ** * * * ***
* * * * * *** * * *
* *** * * ** * * * * << "Sorted integer array: ";
* * ** *** *** *** * **
* ** ** *** ** * ***** * ** ** * * ****
* **** * *** *** * * * *** *
* * * * ** * * *** * * << "Sorted float array: ";
* ** *** * * * * *** * *** * **** ***
* * * * * ** * * ******* * * ** * **
* ****** *** * * ** * * * * ** *** * *
** * ** **** ** << "Sorted char array: ";
* *** ** * * * ***** * *** **** *
* * *** ** ** * ** * *** * ** *** * *
*** * * **** ** **** * *** **
* ** * * * * ** << "Sorted string array: ";
* ** *** * ** **** *** ** * *

}
answered by (-384 points)
0 like 0 dislike
Hidden content!
#include ** ** ** ** ***

using namespace std;



template <class a>



void insert_sort(a b[], int n)

{
* *** * ** ** ** x;
***** * ***** * **** **** i=1; i<n; i++)
** *** ** * * *****
** *** * * * * * * *** * * **** **** = b[i];
* **** * * *** * * * * ** ** ** * *** ****** ** * j=i-1;
* ** * ** * * ** *** * ** * * ** * ** * >= 0 * *** ** b[j] > x)
** **** ** * * * * * * * * ** ** * ** **
* ** * * ** * ** * * * *** ** * * * *** * ****** **** ** ** * = b[j];
* * * * *** ** * * * * * **** ** * * * * * * * * *** ** * * **** *
* **** ** * ** * ** *** ****** * ** ***
* * *** * * *** ** *** * * ** * * *** ** * * = x;
* ** **** ** *** ********

}

template <class a>

void show_items(a b[], int n)

{
**** ** ** * * *** ***** ** * ** i=0; i<n; i++){
**** ** * ** *** ** * * * ** * *** ** * * * **** << b[i];
** * *** * ** * ** ****** * * **** ** * * ***** ** ** ** *
** ***** * * * *** * *** *** * * ** * * * * *** ** << ' ';}

}







int main()

{
* *** **** ** ** * * * ** farr[] = *** ** **** ** *
** * * * * * * iarr[] = *** ***
* *** *** * ** *** ** carr[]= * ** ** * *** *
* **** * ** ** **** ** sarr[]= **** ** * * * * ****** * * ** * *** ** ********* ** ** ** * * * *** * *** *** * * ***** ** ** ** * * **** *
** ** ****** * * ** ******* * ** **
** ** ** ** *** * * * ** << "Sorted integer array: ";
** * * * * * * * ***** *
**** * **** ** ** *** ** * * *
** * ** *** ** * *** * *
* *** **** * * ** * * ***** << "Sorted float array: ";
****** * ** * *** **** * ** * ** **
* * ** ** ** ** ** *** * * ****
* *** * **** * ** * * * ** * * *
* * * ** ** * *** ** << "Sorted char array: ";
** ****** * *** * ** * * ***
* * * ** ** *** **** *** *** * ** **
* *** * ** ** *** ** * * *
*** * ** *** * *** ** * ** **** << "Sorted string array: ";
** ** * * * * **** * ** *** * ****


** * * * **** ** ** ** 0;

}
answered by (-364 points)
0 like 0 dislike
Hidden content!
#include * * ** * *



using namespace std;



template <class T>

void insert_sort(T *items, int size)

{
** * * ** * ** ** * x;
* ** * * * **** ** ** * j;
**** * ** ** **** * i=1; i<size; i++)
* ** * * **
** * * * * * * ***** * ***** * ** ** * = items[i];
* ** ** * ***** *** * ** ** * * * = i - 1;
** ** * * * *** * * ** ***** * ** * * ** (j >= 0 *** * * *** items[j] > x)
* * * * * * ************* *** *** * * **
* ** ** * ** * ** *** ** * * ** ***** * * ** * * * * ** * *** * = items[j];
* * *** * ** **** * *** * ******* ** ** ***** ** * * * ** ** * ** * = j - 1;
** * * * * * ***** * ** *** *** *** *
*** * *** **** ** * * ** * ** ******** ** * = x;
***** * * ** *** ******

}



template <class T>

void show_items(T *items, int size)

{
* ********** ** ** * i=0; ** *** * * i++)
* * ***** * * *** * ****** ***** * ** *** * * *** * items[i] << " ";
** * * * * ****** ** * * * * * * items[size-1];

}



int main()

{
* * ** *** * farr[] = ***** * * * *** *
* ** * * * * **** *** ** iarr[] = ** * * **
** ** *** * ** **** ** *** carr[]= ** * * ** ** ** ** *****
*** *** * * * * * *** * sarr[]= * * ***** * * * * * * * *** * ** * * *** * ** * * ** * ** ** * * *** *** *** * ** ******* ** * ** * ****
****** * **** *** ** ** **
** *** * * * ***** * *** * ** * * ***** * * ** integer array: ";
* * * * ** *** **** * *** *
* ** * * * * ** * * *******
****** ** * *** ** *** * ** * ** **
** * ** **** * << "Sorted float array: ";
** ** * ** * *** * * ** ** **
*** *** * * * ** ** * * *** * ***
* ** *** * ** * * *** ** * * * ** **
* * * * * ** * *** ** << "Sorted char array: ";
* * *** * *** * *** * ** *
* * ** * *** * * ** ** ** *** * ** ** **
* * * ** **** * ** * *** ****
** * * * * * ** * *** * *** * * **** string array: ";
******* ** ** * **** * * * * ** *

}
answered by (5.2k points)
0 like 0 dislike
Hidden content!
#include ** **** **

using namespace std;



template <class a>



void insert_sort(a b[], int n)

{
** ** * ** * * ******* * x;
* * * * * ** ** * ** * ** i=1; i<n; i++)
* * ********* ***
** * ***** ** ** * ****** *** * * ** = b[i];
*** ** ***** **** * ** * *** ** * * j=i-1;
* ******* *** ** ** * * * ******* * ** ** * * >= 0 ** * b[j] > x)
* * * ** *** * ** *** ** ** * *** ****** * ** ***
** * ****** * ******* **** * ** * *** * ***** ** **** * ***** * * *** * = b[j];
** ** * ** * * * ** * * * * ** *** * * * **** *** * ** ** * *** * *
** * * * ** ** *** ** **** * * ***
** ** *** ** *** * **** * ***** * * * * ** * = x;
* ** * * ** * **

}

template <class a>

void show_items(a b[], int n)

{
***** **** ** **** i=0; i<n; i++)
* * ** ** *** * * * ** **** **** *** ** * << b[i] << ' ';

}







int main()

{
* ** * * ****** ** * ** farr[] = *** ** ** *** ******
* * * ** *** *** *** * ** iarr[] = ** *** **
**** ** *** **** ** * carr[]= * * ** ** ****
**** * ** * * ** * ** sarr[]= ** *** *** *** * ****** *** ******* * * * * ** **** * * * * * *** ** * ** ** ** * *** * *** *** * * ** * *** ** ** **
** * **** * * * * *** ********* ****** *
*** ***** ** ** **** ** * **** ** * * * integer array: ";
**** * * * * * * ** * * * ******
* * ** * **** ** * * ***** * * ** ** *
* * * * * * ***** ** * * * * *
* * ***** * * * *** << "Sorted float array: ";
** * * **** * ** ** ***** ** * *
* ** * ** * * * *** **** * * ** *
* *** * * *** * ** * * **
*** * * ** ** ** ** * << "Sorted char array: ";
* ** * * ** ** *** ***** ******** ***
**** * ** **** * * ***** * *********
* * * * *** * * ** * ** * *
** ** * ** * *** *** *** **** << "Sorted string array: ";
* * * **** *** * *** *


*** ** * **** * * * ** 0;

}
answered by (-364 points)
0 like 0 dislike
Hidden content!
#include * ** *** *



using namespace std;



template <class T>

void insert_sort(T *items, int size)

{
* * ** * *** *** * * x;
**** ***** * ** * ** j;
******* *** * * * * * ** * i=1; i<size; i++)
** ** ** * **** ***** **
*** * * * ****** * ** ** * = items[i];
*** * * ** *** * *** **** *** **** *** = i - 1;
* * ** ** *** **** *** * * ** ** * ** ** *** (j >= 0 *** *** * items[j] > x)
* *** ** * ****** * * **** *** ** **
**** **** ***** ****** * ** ***** * * ** * * * * * ** * * * ** * ** * = items[j];
****** * ** ** ** ** ***** * **** * * ** * * *** *** = j - 1;
** * * * * ** * * *** * * ***** * ***
***** ** *** * **** **** * ** * *** * * * *** * = x;
* ** * *** **

}



template <class T>

void show_items(T *items, int size)

{
* * ****** * *** ** i=0; i<size; i++)
* * *** * *** ** *** *** *** **** *** * ** items[i] * ** ** ** " ";

}



int main()

{
* * * * ** ** ** *** farr[] = * * * ****
* ** * * ** * * ** iarr[] = ** **** *
* ** ** *** ** * * carr[]= ****** * * *** *******
*** * * **** **** ** sarr[]= * * * * * * * * * ** * * **** **** * * * * * * * ** **** * * * ** ** *** ****** * * * * ** **
* * ** * * ** **** * ***
* * ** ** ***** * ** * * * "Sorted integer array: ";
** *** ** * ** * ** *
* **** * * * *** * *** * ** ** *
* * * *** * ** ** * ** * * * ** * *
** * *** ** ******** * ** ** *** ** ** * * ****** float array: ";
****** * ** ** * *** * *** ***
****** * *** ** * * ** * *
** ** * * ** ** * * *
** ** * * *** * * ** << "Sorted char array: ";
* *** * * * * **** * **** * * *
* **** ******* * * ** ** **** ** * *
***** ** * *** * ******* ** * ****
*** *** * ** ******* * ***** << "Sorted string array: ";
* * ** * ** * ** * * **** *

}
answered by (5.2k points)
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:104.23.197.65
©2016-2026

Related questions

0 like 0 dislike
15 answers
[Exercise] Coding (C) - asked Jun 1, 2017 in C++
ID: 24854 - Available when: Unlimited - Due to: Unlimited
| 3.8k views
0 like 0 dislike
22 answers
[Exercise] Coding (C) - asked Jun 8, 2017 in C++
ID: 24895 - Available when: Unlimited - Due to: Unlimited
| 4.7k views
0 like 0 dislike
17 answers
[Exercise] Coding (C) - asked Jun 1, 2017 in C++
ID: 24855 - Available when: Unlimited - Due to: Unlimited
| 3.8k views
12,783 questions
183,442 answers
172,219 comments
4,824 users