Programming9
  • Flowcharts
  • Programs
    • C Programs
    • C++ Programs
    • Java Programs
    • Python Codes
    • HTML Codes
    • Java Script Codes
    • SQL Codes
  • Tutorials
    • Java Tutorials
    • Competitive Programming
    • Python Tutorials
    • C Programming
  • Blog
  • Login
  1. You are here:  
  2. Home
  3. Tutorials
  4. Competitive Programming

Search Algorithms: Sequential Search vs Interval Search

Details
Written by: Sudhiksha.Malla
  • competitive programming
  • search algorithm
  • sequential search
  • interval search

Many times we come across questions that require us to check for an element or retrieve its position in the array.

In such questions, using an effective Search Algorithm helps us in reducing the time complexity of our code.

 

There are two types of Search Algorithms:

  1. Sequential Search
  2. Interval Search

Read more: Search Algorithms: Sequential Search vs Interval Search

Segregate 0s and 1s in an Array

Details
Written by: Dheepthi Racha
  • Sorting and Searching
  • Java Sorting
  • C++ Sortings

Segregate 0's and 1's in an array

Given an array arr[] containing 0’s and 1’s (only),  segregate the array such that all 0’s are on the left side and all 1’s are on the right side of the array. 

Approach:

For the given array, find the sum of the array which gives the total count of 1’s in the array. Now, subtract that value from the size of the array, which gives a count of 0’s in the array. Print 0’s and 1’s with respect to their count values respectively.

For example,

doc3

Time complexity:  O(n)

Program:

{tab title="C++" class="green"}


/* This code is contributed by Tangudu Sai Kiran. */
//C++ program to sort array containing only 0’ s and 1’s
#include <bits/stdc++.h>
using namespace std;
void sortArray(int n, int arr[])
{
    int no_of_ones = 0;
    
    //counting total number of 1’s
    for(int i = 0; i < n; i++)
    no_of_ones = no_of_ones + arr[i];
    
    //finding total count of 0’s
    int no_of_zeros = n - no_of_ones;
	 
    //displaying the output array
    for(int i = 0; i < no_of_zeros; i++)
        cout<<"0 ";
        
    for(int i = 0; i < no_of_ones; i++)
        cout<<"1 ";
}
int main()
{
    int arr[]={0, 1, 0, 1 ,0 ,1 ,0};
    int n = sizeof(arr)/sizeof(arr[0]);
    //function call
    sortArray(n, arr);                                   
    return 0;
}

{tab title="Java" class="orange"}


/* This code is contributed by Tammisetti Naveen */
//JAVA program to sort array containing only 0’s and 1’s
public class Main {
    public static void SegregateOperation(int n,int arr[])
	{
		int total_sum=0, no_of_zeroes=0;
		
		//computing sum of the array
		for(int i = 0; i < n; i++)
			total_sum += arr[i];
		no_of_zeroes = n - total_sum;
		
		System.out.print("Array after Segregation of 0's and 1's: " );
		for(int i = 0; i < no_of_zeroes; i++)
			System.out.print("0 ");
		for(int i = no_of_zeroes; i < n; i++)
			System.out.print("1 ");
	}
	
	public static void main(String[] args) {
		int arr[]= {0,1,0,0,1,0,1};
		int n=arr.length;
		SegregateOperation(n,arr);
	}

}

{tab title="python" class="blue"}

# Code written by arun reddy

def segregateOperation(array):
    total_sum,no_of_zeroes=0,0
    n=len(array)
    for i in array:
        total_sum+=i
    print("Array after Segregation of 0's and 1's: ",end=" ")
    no_of_zeroes=n-total_sum
    for i in range(no_of_zeroes):
        print(0,end=" ")
    for i in range(no_of_zeroes,n):
        print(1,end=" ")
if __name__=='__main__':
    array=[0,1,1,0,1,0,1]
    segregateOperation(array)

{/tabs}

Output:

Array after segregation is : 0 0 0 0 1 1 1

Contributors for this Article: Tangudu Sai Kiran, Tammisetti Naveen, Arun Reddy

  1. Moore's Voting Algorithm
  2. Finding the Number Occurring Odd Number of Times in an Array
  3. Pair With Given Sum In An Array
  4. Find the Maximum Consecutive One's in Array
  5. Range Sum Queries on Array in O(1) Time Complexity
  6. Sieve of Eratosthenes - Fastest Algorithm to Find Prime Numbers
  7. Primality Test
  8. Ternary Search Algorithm With Example

Page 1 of 3

  • 1
  • 2
  • 3
programming9.com

Programs and tutorials on Programming and Problem Solving.

C C++ Java Python JavaScript SQL HTML

Programs

  • C Programs
  • C++ Programs
  • Java Programs
  • Python Codes
  • HTML Codes
  • JavaScript Codes
  • SQL Codes

Tutorials

  • Java Tutorials
  • Competitive Programming
  • Python Tutorials
  • C Programming
  • Flowcharts
  • Blog

Site Links

  • Home
  • Login
  • About Us
  • Contact
  • Privacy Policy
  • Sitemap