Register Now

Login


Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login


Register Now

Welcome to Our Site. Please register to get amazing features .

Insertion sort coding implementation (Eve – 28 intake section 02)

Write a program for implementing insertion sort.

About Ask me anything


Follow Me

Comments ( 15 )

  1. //Insertion Sort in C++

    //17181203035-28-eve-2

    #include <iostream>
    using namespace std;
    int main()
    {
    int arr[] = { 12, 11, 13, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]); //How it calculates value of n?

    int i, key, j;
    for (i = 1; i < n; i++)
    {
    key = arr[i];
    j = i – 1;

    while (j >= 0 && arr[j] > key)
    {
    arr[j + 1] = arr[j];
    j = j – 1;
    }
    arr[j + 1] = key;
    }

    for (i = 0; i < n; i++)
    cout << arr[i] << ” “;
    cout << endl;
    return 0;
    }

    • sizeof() একটি বিশেষ অপারেটর যা মেমরির মধ্যে কোনও ধরণের সঠিক আকার খুঁজতে ব্যবহৃত হয়।
      sizeof() অপারেটর একটি পূর্ণসংখ্যার অর্থ ফেরৎ বা মান বা অভিব্যক্তি উপস্থাপনের জন্য মেমরিতে
      প্রয়োজনীয় মোট বাইটগুলি প্রদান করে। sizeof() প্রোগ্রামারদের দ্বারা অপারেটরটি অনেক বেশি
      ব্যবহৃত হয়।
      আমরা জানি int এর সাইজ ৪ বাইট, আর এইটা বের করার জন্যে আমরা sizeof() ব্যাবহার করতে পারি

      #include <stdio.h>
      int main()
      {
      int i;
      printf(“size of int = %dnn”, sizeof(i));
      return 0;
      }

  2. /*Insertion Sort*/
    /*ID- 17181203063*/

    #include<stdio.h>
    #include<math.h>

    int main()
    {
    int ary[]={5,4,3,2,1};

    int n= sizeof(ary)/sizeof(ary[0]);  /* No Idea */

    int i,key,j;
    for(i=1;i<n;i++)
    {
    key=ary[i];
    j=i-1;
    while(j>=0 && ary[j]>key)
    {
    ary[j+1]= ary[j];
    j=j-1;
    }
    ary[j+1]=key;
    }

    int p;
    for(p=0;p<n;p++){
    printf(“%d “,ary[p]);
    }

    printf(“n”);

    return 0;
    }

    • sizeof() একটি বিশেষ অপারেটর যা মেমরির মধ্যে কোনও ধরণের সঠিক আকার খুঁজতে ব্যবহৃত হয়।
      sizeof() অপারেটর একটি পূর্ণসংখ্যার অর্থ ফেরৎ বা মান বা অভিব্যক্তি উপস্থাপনের জন্য মেমরিতে
      প্রয়োজনীয় মোট বাইটগুলি প্রদান করে। sizeof() প্রোগ্রামারদের দ্বারা অপারেটরটি অনেক বেশি
      ব্যবহৃত হয়।
      আমরা জানি int এর সাইজ ৪ বাইট, আর এইটা বের করার জন্যে আমরা sizeof() ব্যাবহার করতে পারি

      #include <stdio.h>
      int main()
      {
      int i;
      printf(“size of int = %dnn”, sizeof(i));
      return 0;
      }

  3. /* Insertion sort ascending order */
    /* Id NO: 17181203047, 28/2(eve) */
    #include
    int main()
    {
    int n, array[]={5,4,3,2,1};

    printf(“Enter number of elementsn”);
    scanf(“%d”, &n);

    printf(“Enter %d integersn”, n);

    int i, key, j;
    for(i=1;i<n;i++)
    scanf("%d", &array[i]);

    for(i=1;i 0 && array[j-1] > array[i]) {
    key=array[i];
    j=i-1;
    while(j>0 && array[j]>key)
    {
    array[j+1]= array[j];
    j=j-1;
    }
    array[j+1]=key;
    }
    }
    for(i=1;i<=n;i++) {
    printf("%d", array[i]);
    printf("Sorted list in ascending order:n");
    }
    return 0;
    }

  4. /* Insertion sort ascending order */
    /* id: 17181203067 28/02(eve) */
    #include <stdio.h>
    #include<math.h>
    int main()
    {
    int n, array[10000];

    printf(“Enter number of elements: “);
    scanf(“%d”, &n);

    printf(“Enter %d integers: “, n);

    int p;

    for(p=0;p<n;p++){
    scanf(“%d”,&array[p]);
    }

    int i, key, j;

    for(i=1;i<n;i++)
    {
    key=array[i];
    j=i-1;
    while(j>=0 && array[j]>key)
    {
    array[j+1]= array[j];
    j=j-1;
    }
    array[j+1]=key;
    }

    printf(“Sorted list in ascending order: “);
    int x;
    for(x=0;x<n;x++) {
    printf(” %d “, array[x]);
    }
    return 0;
    }

  5. //ID-> 17181203052
    #include <stdio.h>
    #include<math.h>
    int main()
    {
    int n, array[500];
    printf(“Enter Size of Array: “);
    scanf(“%d”, &n);
    printf(“Input %d integer data: “, n);
    int k;
    for(k=0;k<n;k++){
    scanf(“%d”,&array[k]);
    }

    int i, key, j;

    for(i=1;i<n;i++)
    {
    key=array[i];
    j=i-1;
    while(j>=0 && array[j]>key)
    {
    array[j+1]= array[j];
    j=j-1;
    }
    array[j+1]=key;
    }

    printf(“Insertion Sorted Data: “);
    int x;
    for(x=0;x<n;x++) {
    printf(” %d”, array[x]);
    }
    return 0;
    }

  6. ID >> 17181203056

    #include<iostream>
    using namespace std;
    void display(int *array, int size) {
    for(int i = 0; i<size; i++)
    cout << array[i] << ” “;
    cout << endl;
    }
    void insertionSort(int *array, int size) {
    int key, j;
    for(int i = 1; i<size; i++) {
    key = array[i];
    j = i;
    while(j > 0 && array[j-1]>key) {
    array[j] = array[j-1];
    j–;
    }
    array[j] = key;
    }
    }
    int main() {
    int n;
    cout << “Enter the number of elements: “;
    cin >> n;
    int arr[n];
    cout << “Enter elements:” << endl;
    for(int i = 0; i<n; i++) {
    cin >> arr[i];
    }
    cout << “Array before show: “;
    display(arr, n);
    insertionSort(arr, n);
    cout << “Array after show: “;
    display(arr, n);
    }

  7. // Insertion sort Implementation in C:
    // 17181203005 (28.2)
    #include <stdio.h>
    #include <stdbool.h>

    #define MAX 7 //defining size of our array

    int intArray[MAX] = {4,6,3,2,1,9,7};

    void printline(int count) {
    int i;

    for(i = 0;i < count-1;i++) {
    printf("=");
    }

    printf("=n");
    }

    void display() {
    int i;
    printf("[");

    // navigate through all items
    for(i = 0;i < MAX;i++) {
    printf("%d ",intArray[i]);
    }

    printf("]n");
    }

    void insertionSort() {

    int valueToInsert;
    int holePosition;
    int i;

    // loop through all numbers
    for(i = 1; i < MAX; i++) {

    // select a value to be inserted.
    valueToInsert = intArray[i];

    // select the hole position where number is to be inserted
    holePosition = i;

    // check if previous no. is larger than value to be inserted
    while (holePosition > 0 && intArray[holePosition-1] > valueToInsert) {
    intArray[holePosition] = intArray[holePosition-1];
    holePosition--;
    printf(" item moved : %dn" , intArray[holePosition]);
    }

    if(holePosition != i) {
    printf(" item inserted : %d, at position : %dn" , valueToInsert,holePosition);
    // insert the number at current hole
    intArray[holePosition] = valueToInsert;
    }

    printf("Iteration %d#:",i);
    display();

    }
    }

    void main() {
    printf("Input Array: ");
    display();
    printline(50);
    insertionSort();
    printf("Output Array: ");
    display();
    printline(50);
    }

  8. ID: 15163203102

    #include<iostream>
    using namespace std;

    int main(){
    int i,j,key,arry[5];
    for(int b = 0; b<5; b++){
    cout<<“nt Please input number “<<b+1<<“: “;
    cin>>arry[b];
    }
    cout<<“nt The array is now: “<<endl;
    for(int b = 0; b<5; b++){
    cout<<“t “<<arry[b];
    }
    int n= sizeof(arry)/sizeof(arry[0]);
    for(i=1;i<n;i++){
    key=arry[i];
    j=i-1;
    while(j>=0 && arry[j]>key)
    {
    arry[j+1]= arry[j];
    j=j-1;
    }
    arry[j+1]=key;
    }
    cout<<“nnt After final sorting the array is now: “<<endl;
    for(int b = 0; b<5; b++){
    cout<<“t “<<arry[b];
    }

    return 0;
    }

  9. id- 17181203058

    #include<iostream>

    using namespace std;

    int main()
    {
    int i,j,n,key,a[5];
    cout<<“Enter the number of elements:”;
    cin>>n;
    cout<<“nEnter the elementsn”;

    for(j=0;j<n;j++)
    {
    cin>>a[j];
    }

    for(j=1;j<=n-1;j++)
    {
    key=a[j];
    i=j-1;

    while((key<a[i])&&(i>=0))
    {
    a[i+1]=a[i];

    i=i-1;
    }

    a[i+1]=key;
    }

    cout<<“nSorted list is as followsn”;
    for(i=0;j<n;j++)
    {
    cout<<a[j]<<” “;
    }

    return 0;
    }

  10. // id number: 17181203047 //

    #include <stdio.h>
    #include<math.h>
    int main()
    {
    int n, array[10000];
    printf(“Enter number of elements: “);
    scanf(“%d”, &n);
    printf(“Enter %d integers: “, n);
    int p;
    for(p=0;p<n;p++){
    scanf(“%d”,&array[p]);
    }
    int i, key, j;
    for(i=1;i<n;i++)
    {
    key=array[i];
    j=i-1;
    while(j>=0 && array[j]>key)
    {
    array[j+1]= array[j];
    j=j-1;
    }
    array[j+1]=key;
    }
    printf(“Sorted list in ascending order: “);
    int x;
    for(x=0;x<n;x++) {
    printf(” %d “, array[x]);
    }
    return 0;
    }

  11. /* Insertion sort ascending order */
    /* id: 17181203049 28/02(eve) */
    #include
    #include
    int main()
    {
    int n, array[10000];

    printf(“Enter number of elements: “);
    scanf(“%d”, &n);

    printf(“Enter %d integers: “, n);

    int p;

    for(p=0;p<n;p++){
    scanf("%d",&array[p]);
    }

    int i, key, j;

    for(i=1;i=0 && array[j]>key)
    {
    array[j+1]= array[j];
    j=j-1;
    }
    array[j+1]=key;
    }

    printf(“Sorted list in ascending order: “);
    int x;
    for(x=0;x<n;x++) {
    printf(" %d ", array[x]);
    }
    return 0;
    }

  12. Md Shamol Hossain Biswas

    ID  17181203066

    #include<iostream>
    using namespace std;

    int main(){
    int i,j,key,arry[5];
    for(int b = 0; b<5; b++){
    cout<<“nt Please input number “<<b+1<<“: “;
    cin>>arry[b];
    }
    cout<<“nt The array is now: “<<endl;
    for(int b = 0; b<5; b++){
    cout<<“t “<<arry[b];
    }
    int n= sizeof(arry)/sizeof(arry[0]);
    for(i=1;i<n;i++){
    key=arry[i];
    j=i-1;
    while(j>=0 && arry[j]>key)
    {
    arry[j+1]= arry[j];
    j=j-1;
    }
    arry[j+1]=key;
    }
    cout<<“nnt After final sorting the array is now: “<<endl;
    for(int b = 0; b<5; b++){
    cout<<“t “<<arry[b];
    }

    return 0;
    }

Leave a reply

Captcha Click on image to update the captcha .