2

my problem is for print n largest element from array.

  1. use single loop
  2. value dynamic(I change array and length and number that last 3/5/10),
  3. not use array sorting or bubble sorting.
  4. make a function

I tried this program

#include <stdio.h>
int main()
{
    int length;
    int data[] = {5, -2, 0, -3, 3}; //.it can be changable
    length = sizeof(data) / sizeof(int);
    int i = 0;
    int number = 3; //.it mean print how many largest number to print.
    int temp;
    
    for (i = 0; i < length; i++)
    {
        if (data[i] > data[i+1])
        {
            temp = data[i];
            data[i] = data[i+1];
            data[i+1] = temp;
            
        }
        else
        {
            temp = data[i+1];
            data[i+1] = data[i];
            data[i] = temp;
        }
    }
    printf("%d",data[i]);

    return 0;
}

my output is 3.

S.G
  • 129
  • 11
  • 2
    Your loop is sorting the array. – Barmar Aug 10 '21 at 06:00
  • @kaylum I think they mean Nth largest. – Barmar Aug 10 '21 at 06:01
  • Actually, your loop swaps the elements in both the `if` and `else` blocks. – Barmar Aug 10 '21 at 06:01
  • @Barmar I hope you understand my answer tell me answer in type of code and not Nth largest number I have to get N largest number – S.G Aug 10 '21 at 06:03
  • After the loop is done, `data[i]` is accessing outside the array, so you have undefined behavior. – Barmar Aug 10 '21 at 06:03
  • Why doesn't your program ever use `number`? – Barmar Aug 10 '21 at 06:04
  • You never increment `j`. So all you're doing is swapping the first element of the array with the `i`th element repeatedly. – Barmar Aug 10 '21 at 06:05
  • hmm... so the 4 items are constraints!? I don't understand #2. Can you explain – Support Ukraine Aug 10 '21 at 06:12
  • ... and assuming the 4 items are constraints, it seems you ignore #3 and #4. And further I don't seen any attempt to print the **n** largest elements - you only print 1. – Support Ukraine Aug 10 '21 at 06:16
  • yes without array sorting you have to code for print N largest element not Nth largest, using single loop and make a function. I can change in array to n element and print N largest element – S.G Aug 10 '21 at 06:16
  • i understand that in my program have many mistake so, i come here if you understand my question tell me code or logic @4386427 – S.G Aug 10 '21 at 06:18
  • @S.G The problem is I don't fully understand your task so I can't help. Especially I have no idea what this is mean: "value dynamic(I change array and length and number that last 3/5/10)," – Support Ukraine Aug 10 '21 at 06:25
  • @4386427 it means that i change in array and change in N largest element of array like array[100] = {1,2,3,4,....,100} and i have to get largest 50 element of the array without sorting of array then your program have to give output in sequence of 51,52,53 up to 100 – S.G Aug 10 '21 at 06:28
  • hmm... I'll give it a try - hang on – Support Ukraine Aug 10 '21 at 06:49
  • 1
    @S.G Review `for (i = 0; i < length; i++) { if (data[i] > data[i+1])`. `data[i+1]` may access outside the array. – chux - Reinstate Monica Aug 10 '21 at 14:35

2 Answers2

0

An obvious solution would be to sort the array but...

Given the constrains:

  1. use single loop
  2. Array size and number of elements is dynamic
  3. do not use array sorting or bubble sorting.
  4. make a function

You can do:

void print_largest_ints(int* p, unsigned nelem, unsigned n)
{
    if (n == 0) return;
    if (n > nelem) n = nelem;
    unsigned index = 0;
    for (unsigned i = 1; i < nelem; ++i)
    {
        if (p[i] > p[index])
        {
            index = i;
        }   
    }
    
    printf("%d ", p[index]);
    int saved = p[index];
    p[index] = INT_MIN;
    print_largest_ints(p, nelem, n-1);
    p[index] = saved;
}

and call it like

int main(){
    int arr[] = {7, 3, 5, 9};
    unsigned elements_to_print = 2;
    print_largest_ints(arr, sizeof arr / sizeof arr[0], elements_to_print);
    return 0;
}

Output:

9 7

This should be inaccordance with your constraints, i.e.

  • It's implemented in a function (#4)
  • The function can accept any array size and N-value (#2)
  • The function has a single loop (#1)
  • and the array isn't sorted (#3)

The array is changed while the function is executed but the array is unchanged when the function returns to main.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
0

You could use the quickselect algorithm to find the n-th largest element in an unordered array. It is based on the quick sort sorting algorithm but instead of doing a complete quicksort it stops at the point where pivot itself is the n-th largest element. Also, it does not to recurse for both left and right sides of pivot, but only recurses for the part that contains the n-th largest element.

Here's an implementation in C:

#include <stdio.h>
#include <limits.h>

void swap(int *a, int *b)
{
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

int partition(int arr[], int l, int r)
{
    // use last element as pivot
    int x = arr[r], i = l;

    // move larger elements to left of pivot and smaller elements to right of it
    for (int j = l; j <= r - 1; j++) {
        if (arr[j] >= x) {
            swap(&arr[i], &arr[j]);
            i++;
        }
    }
    swap(&arr[i], &arr[r]);
    return i;
}

int nthLargest(int arr[], int l, int r, int n)
{
    // if n is smaller than number of elements in array
    if (n > 0 && n <= r - l + 1) {

        // partition the array around last element
        int index = partition(arr, l, r);

        // if position is same as n, return element
        if (index - l == n - 1)
            return arr[index];

        // if position is more, recur for left subarray
        if (index - l > n - 1)
            return nthLargest(arr, l, index - 1, n);

        // otherwise recur for right subarray
        return nthLargest(arr, index + 1, r, n - index + l - 1);
    }

    return INT_MAX;
}

int main()
{
    int data[] = {5, -2, 0, -3, 3, 6, -6};
    int n = sizeof(data)/sizeof(int);
    printf("%d", nthLargest(data, 0, n - 1, 1));
    printf("\n");
    return 0;
}

Output:

6

Demo

Quickselect has O(n) average complexity with worse case of O(n^2).

jignatius
  • 6,304
  • 2
  • 15
  • 30
  • it's sorting method not sorting array and not use bubble sort method – S.G Aug 12 '21 at 04:48
  • @S.G I thought this might be acceptable as it is not fully sorting the array. You might find [this](https://stackoverflow.com/questions/1034846/finding-nth-item-of-unsorted-list-without-sorting-the-list) question interesting. Another possible solution is to build a max-heap. – jignatius Aug 12 '21 at 06:07