Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8972041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:06:59+00:00 2026-06-15T18:06:59+00:00

I have question in my assignment says: Given an unsorted array whose elements are

  • 0

I have question in my assignment says:

Given an unsorted array whose elements are all 0 (zeros) or 1 (ones),
write code to sort the array so that all the 0’s appear first, followed by all the 1’s.
You may assume that ‘num_elements’ always accurately indicates the number of elements
in the array.
Please use the following function header:

void sort_elements(int array[], int num_elements);

I realy have no idea what he means by that, did he want two arrays or just one. Please explain to me.

I did it 😀 I can’t believe myself ,, i used 2 for loops with 2 if in it
.. thank u all for ur assistance guys <3 <3

public class Q4 {

    public static void main (String []args){

         int Sky[]={0,0,1,1,0,1};
         sort_elements(Sky,Sky.length);          
       }

      public static void  sort_elements(int array[],int num_elements){
         for ( int i=0; i < num_elements ; i++){
            if ( array[i]==0){
               System.out.println(array[i]);
            }
        }

         for ( int i=0; i<num_elements ; i++){
            if ( array[i]==1){
               System.out.println(array[i]);
            }
        }



    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:07:01+00:00Added an answer on June 15, 2026 at 6:07 pm

    1) Maintain two pointers left and right.
    2) Start traversing left pointer towards right until 1 is encountered.
    3) Start right pointer towards left until 0 is encountered.
    4) set array[left] = 0, array[right] = 1
    5) Continue doing so until left and right pointer converge.
    6) Since, we have to traverse the array only once, sorting happened in O(n) time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have question about XSLT1.0. The task is to write out in HTML all
This is an assignment question I received from school. The question says, write a
I have a Assignment question about Java Generics. The given class is Product.java. It
Hello is have a question for a school assignment i need to : Read
I have question i.e how can i select a previous div CODE:- if($i >
I have a basic question on array and pointer in C/C++. Say I have:
I have a question regarding restricted pointer assignments. See the comments in code for
I have question regarding polymorphism about its assignment statement, for Example this is The
So, I have a question in my assignment. It's Add fractions (1/10)+(2/9)+(3/8)+(4/7)+(5/6)+(6/5)+(7/4)+(8/3)+(9/2)+(10/1) together and
I have a basic question regarding assignment of a list of subclass to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.