Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to sort a range of int array using qsort in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>

#define LEN 7

void sortRange(int arr[], int start, int len);
void print_array(int arr[], int len);
int compare(const void *a, const void *b);

int main(void)
{
    int arr[LEN] = {8, 3, 5, 2, 0, 1, 7};
   
    sortRange(arr, 2, 4);
    print_array(arr, LEN);
   
    return 0;
}

void sortRange(int arr[], int start, int len)
{
    qsort(arr + start, len, sizeof(int), compare);
}

int compare(const void *a, const void *b)
{
   return ( *(int*)a - *(int*)b );
}
 
void print_array(int arr[], int len)
{
    int i;
      
    for (i = 0; i < len; i++) printf("%4d", arr[i]);
}

/*
run:
   
  8   3   0   1   2   5   7

*/


answered Mar 7, 2015 by avibootz

Related questions

1 answer 279 views
1 answer 193 views
1 answer 137 views
137 views asked Nov 11, 2021 by avibootz
1 answer 206 views
206 views asked Dec 4, 2019 by avibootz
1 answer 234 views
...