#include <stdio.h> void q_sort(int init_arr[11], int lh, int rh); int part (int init_arr[11], int lh, int rh); int main ( ) { int p, ps = 0; int init_arr[10] = {12, 25, 45, 121, 110, 128, 2, 617, 0, 6 }; int sec_lar[10]; printf(" "); printf("\n"); printf("\t \t \t \t"); printf("\n"); printf("\n"); printf("Array elements are "); printf(" "); printf("\n"); for(p=0; p<10;p++) { printf("\t"); printf("%d", init_arr[p]); } printf(" "); printf("\n"); q_sort(init_arr, 0, 9); printf(" "); printf("\n"); printf("Second largest number in the array "); printf(" "); printf("\n"); for(p=9; p>=0; p--) { sec_lar[ps] = init_arr[p]; ps++; } printf("%d", sec_lar[1]); return 0; } void q_sort(int init_arr[10], int lh, int rh) { if ( lh < rh ) { int til =part(init_arr, lh, rh); if( til > 1 ) { q_sort(init_arr, lh, til-1); } if( til+1 < rh) { q_sort(init_arr, til+1, rh); } } } int part(int init_arr[10], int lh, int rh) { int til = init_arr[lh]; while(1) { while(init_arr[lh] < til) { lh++; } while(init_arr[rh]>til) { rh--; } if(lh<rh) { if(init_arr[lh] == init_arr[rh]) { return rh; } int tempo = init_arr[lh]; init_arr[lh] = init_arr[rh]; init_arr[rh] = tempo; } else { return rh; } } }
Output: Array elements are: 12, 25, 45, 121, 110, 128, 2, 617, 0, 6 Second largest number in the Array is: 128
Leave a Reply