Intermediate Steps in Insertion Sort Algorithm

In summary, for Insertion Sort Algorithms, the list is sorted by comparing each element to the previous ones and swapping them if necessary. The intermediate lists at each step can be shown by removing the head elements of the first list and inserting them into the correct position in the second list. This version of the algorithm uses two lists and is different from the commonly used in-place implementation.
  • #1
Joystar77
125
0
Sort with Insertion Sort Algorithms the following list:

329, 364, 373, 334, 320

Give the intermediate lists at each step.

Can somebody please tell me if this is correct for Insertion Sort Algorithms?

329, 364, 373, 334, 320

1. 329, 364, 373, 334, 320

2. 364, 329, 373, 334, 320

3. 329, 373, 364, 334, 320

If this is not correct, can somebody please explain in simpler terms other than saying "2nd number goes before 1st number in array, 3rd number goes imbetween 1st and 2nd number in array." I have a learning disability which is Epilepsy Seizures so its really hard for me to understand when saying something like this. Is there a way to say it in simpler terms?
 
Technology news on Phys.org
  • #2
We begin with the array:

329, 364, 373, 334, 320

It is my understanding of this algorithm that we begin with the second element, and compare it to the first. If it is less than the first, we swap them. In our case we have $329< 364$, so nothing is moved:

1.) 329, 364, 373, 334, 320

For the second step, we look at the third element, and seeing that $364<373$, nothing is done:

2.) 329, 364, 373, 334, 320

For the third step, we look at the fourth element, and we see that $334<373$ and $334<364$ but $329<334$ so we move the fourth element to the second position:

3.) 329, 334, 364, 373, 320

For the fourth and final step, we look at the fifth element and see that $320<373$, $320<364$, $320<334$, and $320<329$, so we move the fifth element to the first position:

4.) 320, 329, 334, 364, 373

Now the list is sorted.
 
  • #3
Even though Wikipedia says that "the common practice is to implement [insertion sort] in-place" (i.e., operating on one array), sometimes the algorithm is presented as operating on two lists: it removes head elements of the first list and inserts them into the correct position in the second list. Thus, the second list is always sorted. In this case, the two lists at each step are as follows.

Code:
Step  First list                Second list
 1    329, 364, 373, 334, 320   empty
 2    364, 373, 334, 320        329
 3    373, 334, 320             329, 364
 4    334, 320                  329, 364, 373
 5    320                       329, 334, 364, 373
 6    empty                     320, 329, 334, 364, 373

The OP should sheck which version of insertion sort is used in his/her sourses and provide more details if necessary.
 
  • #4
Where did you give the intermediate lists at each step? Is it correct then in more simpler terms that in Insertion Sort your comparing the array to what is greater than or less than? Afterwards, then I compare the array and if it's less than/greater than the first array, then I swap them.

MarkFL said:
We begin with the array:

329, 364, 373, 334, 320

It is my understanding of this algorithm that we begin with the second element, and compare it to the first. If it is less than the first, we swap them. In our case we have $329< 364$, so nothing is moved:

1.) 329, 364, 373, 334, 320

For the second step, we look at the third element, and seeing that $364<373$, nothing is done:

2.) 329, 364, 373, 334, 320

For the third step, we look at the fourth element, and we see that $334<373$ and $334<364$ but $329<334$ so we move the fourth element to the second position:

3.) 329, 334, 364, 373, 320

For the fourth and final step, we look at the fifth element and see that $320<373$, $320<364$, $320<334$, and $320<329$, so we move the fifth element to the first position:

4.) 320, 329, 334, 364, 373

Now the list is sorted.

- - - Updated - - -

Where did you give the intermediate lists at each step? Is it correct then for me to understand this better that in Insertion Sort that if I am comparing the array and it's less than or greater than then I would swap the numbers?
Evgeny.Makarov said:
Even though Wikipedia says that "the common practice is to implement [insertion sort] in-place" (i.e., operating on one array), sometimes the algorithm is presented as operating on two lists: it removes head elements of the first list and inserts them into the correct position in the second list. Thus, the second list is always sorted. In this case, the two lists at each step are as follows.

Code:
Step  First list                Second list
 1    329, 364, 373, 334, 320   empty
 2    364, 373, 334, 320        329
 3    373, 334, 320             329, 364
 4    334, 320                  329, 364, 373
 5    320                       329, 334, 364, 373
 6    empty                     320, 329, 334, 364, 373

The OP should sheck which version of insertion sort is used in his/her sourses and provide more details if necessary.
 
  • #5
I assume that after the line that says "Updated" in post #4 you are asking me and not Mark because that part contains my quote.

Joystar1977 said:
Where did you give the intermediate lists at each step?
In the table, which follows the word "Code:" in post #3.

Joystar1977 said:
Is it correct then for me to understand this better that in Insertion Sort that if I am comparing the array and it's less than or greater than then I would swap the numbers?
One cannot compare an array because it consists of many numbers. One can compare a single number to another number. You also did not say which array (or list) you are talking about because my algorithm works with two lists. Finally, you did not say to what you compare the array and which numbers you swap. For these reasons, I am having difficulties understanding your question.

Edit: Perhaps you are asking Mark after all.
 
  • #6
Joystar1977 said:
Where did you give the intermediate lists at each step? Is it correct then in more simpler terms that in Insertion Sort your comparing the array to what is greater than or less than? Afterwards, then I compare the array and if it's less than/greater than the first array, then I swap them...

The numbered lines in my post are the intermediate steps.

I used Wikipedia's description of the algorithm:

Insertion sort - Wikipedia, the free encyclopedia
 
  • #7
Thank you MarkFL!

- - - Updated - - -

Thank you Evgeny.Makarov!
 

1. What is an insertion sort algorithm?

An insertion sort algorithm is a sorting algorithm that works by taking one element at a time from an unsorted list and inserting it into the correct position in a sorted list. It is a simple and efficient algorithm, particularly for small data sets.

2. How does an insertion sort algorithm work?

The insertion sort algorithm works by dividing the original list into two sublists - a sorted sublist and an unsorted sublist. It then iterates through the unsorted sublist, taking one element at a time and inserting it into the correct position in the sorted sublist. This process is repeated until the entire list is sorted.

3. What is the time complexity of an insertion sort algorithm?

The time complexity of an insertion sort algorithm is O(n^2), where n is the number of elements in the list. This means that the algorithm's performance is directly proportional to the square of the input size. It is considered an efficient algorithm for smaller data sets, but can become inefficient for larger data sets.

4. What are the advantages of using an insertion sort algorithm?

One advantage of using an insertion sort algorithm is that it is a simple and easy-to-implement algorithm. It also has a relatively low space complexity, meaning it does not require a lot of additional memory. Additionally, it is efficient for sorting small data sets, particularly if the list is already partially sorted.

5. When should I use an insertion sort algorithm?

An insertion sort algorithm is best used for sorting small data sets or for lists that are already partially sorted. It is also useful in situations where the input list is being continuously updated, as it can efficiently insert new elements into a sorted list. However, for larger data sets, other sorting algorithms such as merge sort or quick sort may be more efficient.

Similar threads

  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
10
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • Programming and Computer Science
Replies
2
Views
8K
  • STEM Academic Advising
Replies
13
Views
2K
Back
Top