Monday, December 2, 2019

Insertion Sort in C++

I wrote this program that will accept ten numbers and then the program will sort the given number and display the sorted values using an insertion sort algorithm using the C++ programming language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.

Here in Bacolod City, Negros Occidental I also accepting computer repair, networking and Arduino Project development at a very affordable price.

My Facebook address is https://www.facebook.com/profile.php?...

My personal website is http://www.jakerpomperada.com

My programming website is http://www.jakerpomperada.blogspot.com

I am also a book author you can purchase my books on computer programming and information technology in the following links below.


https://www.mindshaperspublishing.com/

https://www.unlimitedbooksph.com/

Thank you very much for your help and support.




Sample Program Output


Program Listing

insertion_sort.cpp


#include <iostream>

// insertion_sort.cpp
// Jake R. Pomperada, MAED-IT
// December 2, 2019

#include <iostream>

using namespace std;

void printArray(int array[], int size)
{
 cout <<"\t";
  for (int i = 0; i < size; i++)
  {
    cout <<" " <<array[i] << " ";
  }
  cout << endl;
}
void insertionSort(int array[], int size)
{
  for (int step = 1; step < size; step++)
  {
    int key = array[step];
    int j = step - 1;
    while (key < array[j] && j >= 0)
    {
     
      array[j + 1] = array[j];
      --j;
    }
    array[j + 1] = key;
  }
}

int main()
{
 int data[10];

 cout <<"\n\n";
 cout << "\tInsertion Sort in C++";
 cout <<"\n\n";
 for (int a=0; a<10; a++) {
    cout << "\tEnter item value number " <<a+1 <<" : ";
cin >> a[data]; 
  }
  cout <<"\n\n";
  cout << "\tUnSorted array: \n";
  cout <<"\n\n";
  cout <<"\t";
  for (int a=0; a<10; a++) {
  cout <<" " <<a[data] <<" ";
  }
  cout <<"\n\n";
  int size = sizeof(data) / sizeof(data[0]);
  insertionSort(data, size);
  cout << "\tSorted array: \n";
  cout <<"\n\n";
  printArray(data, size);
}




No comments:

Post a Comment