answersLogoWhite

0


Best Answer

//This is for kth largest element. (So this is for n-k smallest element) //Sudipta Kundu [Wipro Technologies] #include <stdio.h> //Input: array with index range [first, last)

//Output: new index of the pivot. An element in the middle is chosen to be a pivot. Then the array's elements are

//placed in such way that all elements <= pivot are to the left and all elements >= pivot are to the right.

int positionPivot(int* array, int first, int last); //Input: array with index range [first, last) and integer K (first <= K < last)

//Output: array whose Kth element (i.e. array[K]) has the "correct" position. More precisely,

//array[first ... K - 1] <= array[K] <= array[K + 1 ... last - 1]

void positionKthElement(int* array, int first, int last, int k); int main() {

int array[] = {7,1,8,3,1,9,4,8};

int i;

for (i = 0; i < 8; i++) {

positionKthElement(array, 0, sizeof(array) / sizeof(array[0]),i);

printf("%d is at position %d\n", array[i], i);

}

return 0;

}

int positionPivot(int* array, int first, int last) {

if (first last)

return first; int tmp = (first + last) / 2;

int pivot = array[tmp];

int movingUp = first + 1;

int movingDown = last - 1;

array[tmp] = array[first];

array[first] = pivot;

while (movingUp <= movingDown) {

while (movingUp <= movingDown && array[movingUp] < pivot)

++movingUp;

while (pivot < array[movingDown])

--movingDown;

if (movingUp <= movingDown) {

tmp = array[movingUp];

array[movingUp] = array[movingDown];

array[movingDown] = tmp;

++movingUp;

--movingDown;

}

}

array[first] = array[movingDown];

array[movingDown] = pivot;

return movingDown;

} void positionKthElement(int* array, int first, int last, int k) {

int index;

while ((index = positionPivot(array, first, last)) != k) {

if (k < index)

last = index;

else

first = index + 1;

}

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

1) Use any method you like, for example a bubble sort or a quick sort, to sort the array in ascending order.

2) Pick the kth element.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How will you Write a c program to find the kth smallest element in an array in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write c program to find median?

If you are using an array : sort using qsort() then take middle element.


Write a c program to find the maximum value of 25 element in an array?

int findMax(int *array) { int max = array[0]; for(int i = 1; i &lt; array.length(); i++) { if(array[i] &gt; max) max = array[i] } return max; }


The minimum number of comparisons requied to find the second smallest element in a 1000 element array is?

1010


How are arrays processed?

Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.


How do you write a program which reads a list of ten numbers and print the list in reserve order in c program?

The simplest way is probably to read the numbers into an array and then prints each element of the array starting at the last one and moving backwards.


Write a program in c to find the largest no out of a matrix of order mn?

/* using ellipses (...) to indicate tabs for clarity */ double largest (double *array, int M, int N) { ... int i, j; ... double *element; ... double answer = array[0][0]; ... for (i=0; i&lt;M; i++) { ... ... for (j=0; j&lt;N; j++) { ... ... ... element = array + i*M + j; ... ... ... if (*element &gt; answer) answer = *element; ... ... } ... } ... return answer; }


Write a program to input 10 number array and find smallest and largest number?

#include&lt;stdio.h&gt; void main() { int a[10],n,i,large,small; printf("enter the value of n\n"); scanf("%d",&amp;n); printf("enter the elements\n"); for(i=0;i&lt;n;i++) scanf("%d",&amp;a[i]); large=a[0]; small=a[0]; for(i=1;i&lt;n;i++) { if(a[i]&gt;large) large=a[i]; if(a[i]&lt;small); small=a[i];} printf("largest element in the array id %d\n",large); printf("smallest element in the array is %d\n",small); }


Write a program to find out the address of an element in an array?

== Java does not allow reference to memory locations. == In C: for (i=0; i&lt;n; ++i) printf ("a[%d] is at %p\n", i, &amp;a[i]);


What will happen if you try to write to an array element larger than your array?

The results of that programming error is undefined. You must NEVER EVER write, or EVEN READ an array element beyond the allocated size of the array. Period.I would flunk a student that consistently did this, and I would fire a programmer that did the same.


Write a java program to print the result in the series?

If you have the series stored in an array, you loop through the array and print each array element in turn. Another possibility is to print out the numbers in the series as you generate them. In that case, you may not need to store anything (depending on the series, of course).


Write an Algorithm to delete a last element from the array?

// Assuming you dynamically allocated this array using "new"... delete array[arraysize - 1]; arraysize--;


How do you write a C Program to fill up an Integer Array?

Reference:cprogramming-bd.com/c_page1.aspx# array programming