-
Notifications
You must be signed in to change notification settings - Fork 294
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
Perctapera patch 1 #568
Open
perctapera
wants to merge
4
commits into
dubesar:master
Choose a base branch
from
perctapera:perctapera-patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Perctapera patch 1 #568
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
932ded0
aaded an example to do with recursion
perctapera 605a6e7
contributed to the documentation and added issue to deal with exceptions
perctapera a84dfba
added a singleton design pattern example
perctapera bd11b04
Quick Sort algorithm in Java
perctapera File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import java.util.*; | ||
|
||
class QuickSort { | ||
//selects last element as pivot, pi using which array is partitioned. | ||
int partition(int intArray[], int low, int high) { | ||
int pi = intArray[high]; | ||
int i = (low-1); // smaller element index | ||
for (int j=low; j<high; j++) { | ||
// check if current element is less than or equal to pi | ||
if (intArray[j] <= pi) { | ||
i++; | ||
// swap intArray[i] and intArray[j] | ||
int temp = intArray[i]; | ||
intArray[i] = intArray[j]; | ||
intArray[j] = temp; | ||
} | ||
} | ||
|
||
// swap intArray[i+1] and intArray[high] (or pi) | ||
int temp = intArray[i+1]; | ||
intArray[i+1] = intArray[high]; | ||
intArray[high] = temp; | ||
|
||
return i+1; | ||
} | ||
|
||
|
||
//routine to sort the array partitions recursively | ||
void quick_sort(int intArray[], int low, int high) { | ||
if (low < high) { | ||
//partition the array around pi=>partitioning index and return pi | ||
int pi = partition(intArray, low, high); | ||
|
||
// sort each partition recursively | ||
quick_sort(intArray, low, pi-1); | ||
quick_sort(intArray, pi+1, high); | ||
} | ||
} | ||
} | ||
|
||
class Main{ | ||
public static void main(String args[]) { | ||
//initialize a numeric array, intArray | ||
int intArray[] = {4,-1,6,8,0,5,-3}; | ||
int n = intArray.length; | ||
//print the original array | ||
System.out.println("Original Array: " + Arrays.toString(intArray)); | ||
//call quick_sort routine using QuickSort object | ||
QuickSort obj = new QuickSort(); | ||
obj.quick_sort(intArray, 0, n-1); | ||
//print the sorted array | ||
System.out.println("\nSorted Array: " + Arrays.toString(intArray)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
public class SingleObject { | ||
|
||
//create an object of SingleObject | ||
private static SingleObject instance = new SingleObject(); | ||
|
||
//make the constructor private so that this class cannot be | ||
//instantiated | ||
private SingleObject(){} | ||
|
||
//Get the only object available | ||
public static SingleObject getInstance(){ | ||
return instance; | ||
} | ||
|
||
public void showMessage(){ | ||
System.out.println("Hello World!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
public class SingletonPatternDemo { | ||
public static void main(String[] args) { | ||
|
||
//illegal construct | ||
//Compile Time Error: The constructor SingleObject() is not visible | ||
//SingleObject object = new SingleObject(); | ||
|
||
//Get the only object available | ||
SingleObject object = SingleObject.getInstance(); | ||
|
||
//show the message | ||
object.showMessage(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class HelloWorld{ | ||
|
||
public static void main(String[] args){ | ||
|
||
static int fact(int n) | ||
{ | ||
if (n == 1) | ||
|
||
// base condition | ||
return 1; | ||
else | ||
return n*fact(n-1); | ||
} | ||
public static void main(String[] args) { | ||
int result = fact(10); | ||
|
||
System.out.println("10! = " + result); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the return type be long . Otherwise, would it be able to return fatorial to numbers greater than 100?