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

Implemented the selection sort algo using python and java #473

Closed
wants to merge 1 commit into from
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
54 changes: 54 additions & 0 deletions selection_sort/Java/SelectionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.util.Random;

/*
____ _ _ _ ____ _
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line contains following spacing inconsistencies:

  • Trailing whitespaces.

Origin: SpaceConsistencyBear, Section: all.pyjava.

The issue can be fixed by applying the following patch:

--- a/tmp/tmpz6y7xvz3/selection_sort/Java/SelectionSort.java
+++ b/tmp/tmpz6y7xvz3/selection_sort/Java/SelectionSort.java
@@ -1,7 +1,7 @@
 import java.util.Random;
 
 /*
- ____       _           _   _             ____             _   
+ ____       _           _   _             ____             _
 / ___|  ___| | ___  ___| |_(_) ___  _ __ / ___|  ___  _ __| |_ 
 \___ \ / _ | |/ _ \/ __| __| |/ _ \| '_ \\___ \ / _ \| '__| __|
  ___) |  __| |  __| (__| |_| | (_) | | |  ___) | (_) | |  | |_ 

/ ___| ___| | ___ ___| |_(_) ___ _ __ / ___| ___ _ __| |_
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line contains following spacing inconsistencies:

  • Trailing whitespaces.

Origin: SpaceConsistencyBear, Section: all.pyjava.

The issue can be fixed by applying the following patch:

--- a/tmp/tmpz6y7xvz3/selection_sort/Java/SelectionSort.java
+++ b/tmp/tmpz6y7xvz3/selection_sort/Java/SelectionSort.java
@@ -2,7 +2,7 @@
 
 /*
  ____       _           _   _             ____             _   
-/ ___|  ___| | ___  ___| |_(_) ___  _ __ / ___|  ___  _ __| |_ 
+/ ___|  ___| | ___  ___| |_(_) ___  _ __ / ___|  ___  _ __| |_
 \___ \ / _ | |/ _ \/ __| __| |/ _ \| '_ \\___ \ / _ \| '__| __|
  ___) |  __| |  __| (__| |_| | (_) | | |  ___) | (_) | |  | |_ 
 |____/ \___|_|\___|\___|\__|_|\___/|_| |_|____/ \___/|_|   \__|

\___ \ / _ | |/ _ \/ __| __| |/ _ \| '_ \\___ \ / _ \| '__| __|
___) | __| | __| (__| |_| | (_) | | | ___) | (_) | | | |_
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line contains following spacing inconsistencies:

  • Trailing whitespaces.

Origin: SpaceConsistencyBear, Section: all.pyjava.

The issue can be fixed by applying the following patch:

--- a/tmp/tmpz6y7xvz3/selection_sort/Java/SelectionSort.java
+++ b/tmp/tmpz6y7xvz3/selection_sort/Java/SelectionSort.java
@@ -4,7 +4,7 @@
  ____       _           _   _             ____             _   
 / ___|  ___| | ___  ___| |_(_) ___  _ __ / ___|  ___  _ __| |_ 
 \___ \ / _ | |/ _ \/ __| __| |/ _ \| '_ \\___ \ / _ \| '__| __|
- ___) |  __| |  __| (__| |_| | (_) | | |  ___) | (_) | |  | |_ 
+ ___) |  __| |  __| (__| |_| | (_) | | |  ___) | (_) | |  | |_
 |____/ \___|_|\___|\___|\__|_|\___/|_| |_|____/ \___/|_|   \__|
 
 */

|____/ \___|_|\___|\___|\__|_|\___/|_| |_|____/ \___/|_| \__|

*/

public class SelectionSort {

public static void main(String[] args) {
int[] unsortedArray = createRandomArray(40);
int[] sortedArray = sort(unsortedArray);
printArray(sortedArray);
}

private static int[] sort(int[] a) {
int maxIdx = a.length - 1;
for (int idx = 0; idx < maxIdx; idx++) {
int minIdx = idx;
for (int i = idx + 1; i <= maxIdx; i++) {
if (a[i] < a[minIdx])
minIdx = i;
}

//swap
int tmp = a[idx];
a[idx] = a[minIdx];
a[minIdx] = tmp;
}
return a;
}

private static int[] createRandomArray(int length) {
int[] array = new int[length];
Random rand = new Random();
for (int i = 0; i < array.length; i++)
array[i] = rand.nextInt(1000);
return array;
}

private static void printArray(int[] array) {
System.out.printf("[ ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
if (i < array.length - 1)
System.out.print(", ");
}
System.out.printf(" ]");
}
}
28 changes: 28 additions & 0 deletions selection_sort/Python/selection_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'''
____ _ _ _ ____ _
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line contains following spacing inconsistencies:

  • Trailing whitespaces.

Origin: SpaceConsistencyBear, Section: all.pyjava.

The issue can be fixed by applying the following patch:

--- a/tmp/tmpz6y7xvz3/selection_sort/Python/selection_sort.py
+++ b/tmp/tmpz6y7xvz3/selection_sort/Python/selection_sort.py
@@ -1,5 +1,5 @@
 '''
- ____       _           _   _             ____             _   
+ ____       _           _   _             ____             _
 / ___|  ___| | ___  ___| |_(_) ___  _ __ / ___|  ___  _ __| |_ 
 \___ \ / _ | |/ _ \/ __| __| |/ _ \| '_ \\___ \ / _ \| '__| __|
  ___) |  __| |  __| (__| |_| | (_) | | |  ___) | (_) | |  | |_ 

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W291 trailing whitespace

Origin: PycodestyleBear (W291), Section: all.autopep8.

/ ___| ___| | ___ ___| |_(_) ___ _ __ / ___| ___ _ __| |_
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line contains following spacing inconsistencies:

  • Trailing whitespaces.

Origin: SpaceConsistencyBear, Section: all.pyjava.

The issue can be fixed by applying the following patch:

--- a/tmp/tmpz6y7xvz3/selection_sort/Python/selection_sort.py
+++ b/tmp/tmpz6y7xvz3/selection_sort/Python/selection_sort.py
@@ -1,6 +1,6 @@
 '''
  ____       _           _   _             ____             _   
-/ ___|  ___| | ___  ___| |_(_) ___  _ __ / ___|  ___  _ __| |_ 
+/ ___|  ___| | ___  ___| |_(_) ___  _ __ / ___|  ___  _ __| |_
 \___ \ / _ | |/ _ \/ __| __| |/ _ \| '_ \\___ \ / _ \| '__| __|
  ___) |  __| |  __| (__| |_| | (_) | | |  ___) | (_) | |  | |_ 
 |____/ \___|_|\___|\___|\__|_|\___/|_| |_|____/ \___/|_|   \__|

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W291 trailing whitespace

Origin: PycodestyleBear (W291), Section: all.autopep8.

\___ \ / _ | |/ _ \/ __| __| |/ _ \| '_ \\___ \ / _ \| '__| __|
___) | __| | __| (__| |_| | (_) | | | ___) | (_) | | | |_
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line contains following spacing inconsistencies:

  • Trailing whitespaces.

Origin: SpaceConsistencyBear, Section: all.pyjava.

The issue can be fixed by applying the following patch:

--- a/tmp/tmpz6y7xvz3/selection_sort/Python/selection_sort.py
+++ b/tmp/tmpz6y7xvz3/selection_sort/Python/selection_sort.py
@@ -2,7 +2,7 @@
  ____       _           _   _             ____             _   
 / ___|  ___| | ___  ___| |_(_) ___  _ __ / ___|  ___  _ __| |_ 
 \___ \ / _ | |/ _ \/ __| __| |/ _ \| '_ \\___ \ / _ \| '__| __|
- ___) |  __| |  __| (__| |_| | (_) | | |  ___) | (_) | |  | |_ 
+ ___) |  __| |  __| (__| |_| | (_) | | |  ___) | (_) | |  | |_
 |____/ \___|_|\___|\___|\__|_|\___/|_| |_|____/ \___/|_|   \__|
 
 '''

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W291 trailing whitespace

Origin: PycodestyleBear (W291), Section: all.autopep8.

|____/ \___|_|\___|\___|\__|_|\___/|_| |_|____/ \___/|_| \__|

'''
# author: john2ksonn
# date: 10.28.2018

import random

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code does not comply to PEP8.

Origin: PEP8Bear, Section: all.autopep8.

The issue can be fixed by applying the following patch:

--- a/tmp/tmpz6y7xvz3/selection_sort/Python/selection_sort.py
+++ b/tmp/tmpz6y7xvz3/selection_sort/Python/selection_sort.py
@@ -10,6 +10,7 @@
 # date: 10.28.2018
 
 import random
+
 
 def sort(a):
     max_idx = len(a) - 1

def sort(a):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E302 expected 2 blank lines, found 1

Origin: PycodestyleBear (E302), Section: all.autopep8.

max_idx = len(a) - 1
for idx in range(0, max_idx):
min_idx = idx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line contains following spacing inconsistencies:

  • Trailing whitespaces.

Origin: SpaceConsistencyBear, Section: all.pyjava.

The issue can be fixed by applying the following patch:

--- a/tmp/tmpz6y7xvz3/selection_sort/Python/selection_sort.py
+++ b/tmp/tmpz6y7xvz3/selection_sort/Python/selection_sort.py
@@ -14,7 +14,7 @@
 def sort(a):
     max_idx = len(a) - 1
     for idx in range(0, max_idx):
-        min_idx = idx        
+        min_idx = idx
         for i in range(idx+1, max_idx+1):
             if a[i] < a[min_idx]:
                 min_idx = i

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code does not comply to PEP8.

Origin: PEP8Bear, Section: all.autopep8.

The issue can be fixed by applying the following patch:

--- a/tmp/tmpz6y7xvz3/selection_sort/Python/selection_sort.py
+++ b/tmp/tmpz6y7xvz3/selection_sort/Python/selection_sort.py
@@ -14,7 +14,7 @@
 def sort(a):
     max_idx = len(a) - 1
     for idx in range(0, max_idx):
-        min_idx = idx        
+        min_idx = idx
         for i in range(idx+1, max_idx+1):
             if a[i] < a[min_idx]:
                 min_idx = i

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W291 trailing whitespace

Origin: PycodestyleBear (W291), Section: all.autopep8.

for i in range(idx+1, max_idx+1):
if a[i] < a[min_idx]:
min_idx = i
a[idx], a[min_idx] = a[min_idx], a[idx]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line contains following spacing inconsistencies:

  • Trailing whitespaces.

Origin: SpaceConsistencyBear, Section: all.pyjava.

The issue can be fixed by applying the following patch:

--- a/tmp/tmpz6y7xvz3/selection_sort/Python/selection_sort.py
+++ b/tmp/tmpz6y7xvz3/selection_sort/Python/selection_sort.py
@@ -18,7 +18,7 @@
         for i in range(idx+1, max_idx+1):
             if a[i] < a[min_idx]:
                 min_idx = i
-        a[idx], a[min_idx] = a[min_idx], a[idx]        
+        a[idx], a[min_idx] = a[min_idx], a[idx]
     return a
     
 

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code does not comply to PEP8.

Origin: PEP8Bear, Section: all.autopep8.

The issue can be fixed by applying the following patch:

--- a/tmp/tmpz6y7xvz3/selection_sort/Python/selection_sort.py
+++ b/tmp/tmpz6y7xvz3/selection_sort/Python/selection_sort.py
@@ -18,9 +18,9 @@
         for i in range(idx+1, max_idx+1):
             if a[i] < a[min_idx]:
                 min_idx = i
-        a[idx], a[min_idx] = a[min_idx], a[idx]        
+        a[idx], a[min_idx] = a[min_idx], a[idx]
     return a
-    
+
 
 if __name__ == "__main__":
     rand_list = random.sample(range(1000), 40)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W291 trailing whitespace

Origin: PycodestyleBear (W291), Section: all.autopep8.

return a

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line contains following spacing inconsistencies:

  • Trailing whitespaces.

Origin: SpaceConsistencyBear, Section: all.pyjava.

The issue can be fixed by applying the following patch:

--- a/tmp/tmpz6y7xvz3/selection_sort/Python/selection_sort.py
+++ b/tmp/tmpz6y7xvz3/selection_sort/Python/selection_sort.py
@@ -20,7 +20,7 @@
                 min_idx = i
         a[idx], a[min_idx] = a[min_idx], a[idx]        
     return a
-    
+
 
 if __name__ == "__main__":
     rand_list = random.sample(range(1000), 40)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

Origin: PycodestyleBear (W293), Section: all.autopep8.


if __name__ == "__main__":
rand_list = random.sample(range(1000), 40)
sorted_list = sort(rand_list)
print(sorted_list)