Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
C array sort example 
 

For sorting an array, we have used the qsort function. This function provides the implementation of quicksort algorithm to sort the elements of an array.

 

C array sort example

                         

In this section, you will learn how to sort an array in C.

For sorting an array, we have used the qsort function. This function provides the implementation of quicksort algorithm to sort the elements of an array.


Syntax of the function:
qsort(array, sizeof(array), sizeof(type), comparison_fn).

In the example, we have created a comparison function sort to compare two elements. For this we have passed two parameters (x and y ) which are pointers to elements and will return an int value by comparing them. This method compares each pair of elements. This function can be declared as:
int sort (const void * x, const void * y );

 (*(int*)x - *(int*)y)- The parameters x and y  checks for each pair of elements considering the elements as x and y. If x is found greater than y, then x goes before y otherwise, x goes after y. In case if x = y, then it remains on the same position.

<stdlib.h> - This header file stands for standard library which includes functions involving String, Memory, Environment, Sorting and Searching, Math and Multibyte

Here is the code:

ARRAYSOR.C

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int array[] 903332880498303625 };
int sort(const void *x, const void *y) {
  return (*(int*)x - *(int*)y);
}
void main() {
  clrscr();
  int i;
  qsort(array, 10, sizeof(int), sort);
  for (i=0; i<10; i++) {
    printf("%d ", array[i]);
  }
  getch();
}

Output will be displayed as:

ARRAYSOR.EXE

Download Source Code:

                         

» View all related tutorials
Related Tags: c string io variable rmi ria this length example to ini learn exam ear e il it section can li

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.