Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Misc/sieve_of_eratosthenes
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This program prints all the prime numbers from 2 to n
import java.io.*;
import java.util.*;
class Solution
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(); //n is the number upto which we need to find the prime numbers.
int arr[]=new int[n+1];
for(int i=0;i<=n;i++) //initialize all the indexes with 1.
{
arr[i]=1;
}
for(int i=2;(i*i)<=n;i++) //start from 2 as 2 is the first prime number.
{
if(arr[i]==1)
{

/* replace the value at all indexes that are multiples of j with 0
ultimately we wiil be left with the numbers that are not multiples of any other number.*/

for(int j=i*2;j<=n;j+=i) {
arr[j]=0;
}
}
}
for(int i=2;i<=n;i++) // print the prime numbers in the range.
{
if(arr[i]==1)
System.out.println(i);
}
}
}
34 changes: 34 additions & 0 deletions Sorts/bubble_sort_with_best_time_complexity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.io.*;
import java.util.*;
public class sort
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();//enter size of array
int arr[]=new int[n];//create an array
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt(); //enter array elements
}
int f=0;
do
{
f=0;
for (int j = 0; j < n-1; j++)
{
if (arr[j] > arr[j+1])
{
// swap temp and arr[j]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
f=1;
}
}}while(f==1);
for(int i=0;i<n;i++)
{
System.out.println(arr[i]);//print the sorted array
}
}
}