In this presentation will learn about with a given array using a our logic how we can sort the elements in an increasing order by writing the program in C++
1. Write a program to sort the elements of an array in
ascending order using insertion sort.
2. Agenda
1. Program in c++
2. Array declaration and definition
3. Sort the given array in ascending(increasing
order)
4. Print the array
1. Before sorting
2. After sorting
3. Logic
• Given array for example:
• arr[]={5,3,2,1}
• Compare each element with each other element in the given array
• Place the elements according to their position
• Logic:
• for(i=1;i<n; i++)
• {
• j=i;
• while(j>=1)
• {
• if(a[j]<a[j-1])
• {
• temp=a[j];
• a[j]=a[j-1];
• a[j-1]=temp;
• }
• j=j-1;
• }
• }