473,387 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Which sort is this and is it better than insertion sort?

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<time.h>
  3. #include<conio.h>
  4. #include<assert.h>
  5. int main()
  6. {
  7.  
  8.     clock_t start, stop;
  9.     int n,i,j,temp,a[100];
  10.     scanf("%d",&n);
  11.     for(i=0;i<n;i++)
  12.         scanf("%d",&a[i]);
  13.     double t = 0.0;
  14.     assert((start = clock())!=-1);
  15.     for(i=0;i<n-1;i++)
  16.     {
  17.         for(j=i+1;j<n;j++)
  18.         {
  19.             if(a[j]<a[i])
  20.             {
  21.                 temp=a[i];
  22.                 a[i]=a[j];
  23.                 a[j]=temp;
  24.  
  25.             }
  26.         }
  27.     }
  28.  
  29.     stop = clock();
  30.     t = (double) (stop-start)/CLOCKS_PER_SEC;
  31.  
  32.     for(i=0;i<n;i++)
  33.         printf("%d ",a[i]);
  34.     printf("Run time: %f\n", t);
  35.  
  36.     return(0);
  37. }
  38.  
May 16 '13 #1
2 1451
vijay6
158 100+
Hey depster, your code is for 'Selection Sort'.

is it better than insertion sort?
No. In general Insertion Sort is faster than Selection Sort.
May 16 '13 #2
Yeah,selection sort, but i think is better using vectors ,in this way u won't have to implement the sorting code yourself :)
May 16 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: N. | last post by:
Insertion sort is O(N^2), but I figure I can get it in to O(N log2 N) if the inside loop of the insertion sort is replaced with a binary search. However, I'm having some implimentation problems......
4
by: ashu | last post by:
can anyone tell me that what is the logic of insertion sort. thank you
2
by: Mark Kamoski | last post by:
Hi Everyone-- Please help. I need a code sample for insertion sort. Thank you. --Mark
3
by: Alexander Widera | last post by:
Hi, I have a problem with this code ... (see below) ... I want to sort an instance of MyList ... by MyData.Shortname ... Shortname is of the type string.... how can I sort the entries? Thank...
5
by: Peggy Wu | last post by:
dear all, I am a programmer ,coding in IBM COBOL and the DB is DB2 V6 on OS/390. As below, for the performance issue, Does anyone know which one is better ?? To get a record that field A has a...
6
by: Julia | last post by:
I am trying to sort a linked list using insertion sort. I have seen a lot of ways to get around this problem but no time-efficient and space-efficient solution. This is what I have so far: ...
18
by: xahlee | last post by:
Last year, i've posted a tutorial and commentary about Python and Perl's sort function. (http://xahlee.org/perl-python/sort_list.html) In that article, i discussed a technique known among...
0
Ganon11
by: Ganon11 | last post by:
So far, the Articles sections are filled with slow sorting algorithms. Bubble Sort and Selection Sort, while very easy to comprehend, are relatively slow algorithms at Θ(n^2) running time. Here, I...
1
by: AhmedGY | last post by:
Hi, am trying to build an app that uses the insertion sort method to sort numbers entered in a textbox and display them sorted in a label, so i wrote this inside the sort button click event: ...
1
by: nikhil124 | last post by:
consider this insertion sort algo(from cormen) INSERTION-SORT(A) for j<--- 2 to length do key<--- A >Insert A into the sorted seq A. i <--- j -1 while i >...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.