• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is my insertion sort inefficient ?  RSS feed

 
Mark Putin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could have copied the code for a standard algorithm such as insertion sort, but I wanted to do it on my own to see how well I think. I came up with a working solution below.
Please tell me if this is efficient or not or if I can make improvements. Would this approach ring any alarm bells in an interview ?



Thanks in advance.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The red flag for me is that you have three loops when you only need two. One loop should look at each element in the array and the other loop should swap the element to the left if it's greater than the element we're looking at. When the element to the left is less than the element we're looking at, stop. I can't think of how a third loop does anything except slow the sort down.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!