Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

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

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8579959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:43:26+00:00 2026-06-11T20:43:26+00:00

I’m trying to perform a simple insertion sort in C# but I don’t know

  • 0

I’m trying to perform a simple insertion sort in C# but I don’t know what I’m doing wrong. When I run my program I want it to output a window like this:

before sorting
80 60 40 20 10 30 50 70

 during sorting
60 80 40 20 10 30 50 70
40 60 80 20 10 30 50 70
20 40 60 80 10 30 50 70
10 20 40 60 80 30 50 70
10 20 30 40 60 80 50 70
10 20 30 40 50 60 80 70
10 20 30 40 50 60 70 80

after sorting
10 20 30 40 50 60 70 80

But it’s just showing only the after sorting part. What might I be doing wrong?

This is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        class CArray
        {
            private int[] arr;
            private int upper;
            private int numElements;
            public CArray(int size)
            {
                arr = new int[size];
                upper = size - 1;
                numElements = 0;

            }
            public void insert(int item)
            {
                arr[numElements] = item;
                numElements++;
            }
            public void displayElements()
            {
                for (int i = 0; i <= upper; i++)
                    Console.Write(arr[i] + " ");
                Console.ReadKey();

            }
            public void clear()
            {
                for (int i = 0; i <= upper; i++)
                    arr[i] = 0;
                numElements = 0;

            }

            public void insertionsort() {
            int inner, temp;
            for (int outer = 1; outer <= upper; outer++) { 
              temp = arr[outer];
              inner = outer;
              while (inner > 0 && arr[inner - 1] >= temp) { 
              arr[inner] = arr[inner-1];
              inner -= 1;
              }
                arr[inner] = temp;

            }

        }
        }
        static void Main(string[] args)
        {

            CArray nums = new CArray(10);
            Random rnd = new Random(100);
            for (int i = 0; i < 10; i++)

                nums.insert((int)(rnd.NextDouble() * 100));

            Console.WriteLine("before sorting: ");
            nums.displayElements();

            Console.WriteLine("during sorting: ");
            nums.insertionsort();

            Console.WriteLine("after sorting: ");
            nums.displayElements();

        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:43:27+00:00Added an answer on June 11, 2026 at 8:43 pm

    This works for me…

    public void insertionsort() { 
        int inner, temp; 
    
        for (int outer = 1; outer <= upper; outer++) {  
            displayElements();                          
            Console.WriteLine(); 
    
            temp = arr[outer]; 
            inner = outer; 
            while (inner > 0 && arr[inner - 1] >= temp) {  
                arr[inner] = arr[inner-1]; 
                inner -= 1;
            } 
    
            arr[inner] = temp; 
        } 
    

    Here is the output I get when I add the two lines to your insertsort() method…

    before sorting:
    96 15 66 90 35 94 71 61 34 14

    during sorting:
    96 15 66 90 35 94 71 61 34 14
    15 96 66 90 35 94 71
    61 34 14
    15 66 96 90 35 94 71 61 34 14
    15 66 90 96 35 94 71 61 34 14

    15 35 66 90 96 94 71 61 34 14
    15 35 66 90 94 96 71 61 34 14
    15 35 66
    71 90 94 96 61 34 14
    15 35 61 66 71 90 94 96 34 14
    15 34 35 61 66 71
    90 94 96 14

    after sorting:
    14 15 34 35 61 66 71 90 94 96

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.