Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to print Fibonacci Series with a Twist in Python #764

Merged
merged 5 commits into from
Sep 30, 2021
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Create 26thSept_srijan-singh.java
  • Loading branch information
srijan-singh committed Sep 30, 2021
commit e97c6a7bbee198b7a55d23ce08abf717ef34d22f
85 changes: 85 additions & 0 deletions JAVA/2021/26thSept_srijan-singh.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import java.util.Scanner;

public class MergeSort {

public static void display(int arr[], int length)
{
for(int i=0; i<length; i++)
{
System.out.print(arr[i] + " ");
}

System.out.println("");
}

public static void merge(int arr[], int start, int mid, int end)
{
int length = end - start + 1;

int temp[] = new int[length];

int index = 0, left = start, right = mid+1;

while(left<=mid && right<=end)
{
if(arr[left] <= arr[right])
{
temp[index++] = arr[left++];
}
else
{
temp[index++] = arr[right++];
}
}

while(left<=mid)
{
temp[index++] = arr[left++];
}

while(right<=end)
{
temp[index++] = arr[right++];
}

for(int i=start; i<=end; i++)
{
arr[i] = temp[i-start];
}
}

public static void mergeSort(int arr[], int start, int end)
{
if(start<end)
{
int mid = (start+end)/2;
mergeSort(arr, start, mid);
mergeSort(arr, mid+1, end);
merge(arr, start, mid, end);
}
}

public static void main(String arg[])
{
Scanner scan = new Scanner(System.in);

System.out.print("Enter length of the Array: ");
int length = scan.nextInt();
int arr [] = new int [length];

for(int i=0; i<length; i++)
{
System.out.print("Enter Element "+(i+1)+" : ");
arr[i] = scan.nextInt();
}
scan.close();

System.out.print("Given Array: ");
display(arr, length);

mergeSort(arr, 0, length-1);

System.out.print("Sorted Array: ");
display(arr, length);
}
}