-
Notifications
You must be signed in to change notification settings - Fork 627
Added some Searching and Sorting Algorithms #413
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| def bubblesort(list): | ||
|
|
||
| # Swap the elements to arrange in order | ||
| for iter_num in range(len(list)-1,0,-1): | ||
| for idx in range(iter_num): | ||
| if list[idx]>list[idx+1]: | ||
| temp = list[idx] | ||
| list[idx] = list[idx+1] | ||
| list[idx+1] = temp | ||
|
|
||
|
|
||
| list = [19,2,31,45,6,11,121,27] | ||
| bubblesort(list) | ||
| print(list) | ||
|
|
||
| #OP | ||
| #[2, 6, 11, 19, 27, 31, 45, 121] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| def insertion_sort(InputList): | ||
| for i in range(1, len(InputList)): | ||
| j = i - 1 | ||
| nxt_element = InputList[i] | ||
| # Compare the current element with next one | ||
|
|
||
| while (InputList[j] > nxt_element) and (j >= 0): | ||
| InputList[j + 1] = InputList[j] | ||
| j = j - 1 | ||
| InputList[j + 1] = nxt_element | ||
|
|
||
|
|
||
| list = [19, 2, 31, 45, 30, 11, 121, 27] | ||
| insertion_sort(list) | ||
| print(list) | ||
|
|
||
| #op | ||
| #[2, 11, 19, 27, 30, 31, 45, 121] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| def intpolsearch(values,x ): | ||
| idx0 = 0 | ||
| idxn = (len(values) - 1) | ||
|
|
||
| while idx0 <= idxn and x >= values[idx0] and x <= values[idxn]: | ||
|
|
||
| # Find the mid point | ||
| mid = idx0 +\ | ||
| int(((float(idxn - idx0)/( values[idxn] - values[idx0])) | ||
| * ( x - values[idx0]))) | ||
|
|
||
| # Compare the value at mid point with search value | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't you think the comment would be better inside the function? |
||
| if values[mid] == x: | ||
| return "Found "+str(x)+" at index "+str(mid) | ||
|
|
||
| if values[mid] < x: | ||
| idx0 = mid + 1 | ||
| return "Searched element not in the list" | ||
|
|
||
|
|
||
| l = [2, 6, 11, 19, 27, 31, 45, 121] | ||
| print(intpolsearch(l, 2)) | ||
|
|
||
| #OP | ||
|
|
||
| #Found 2 at index 0 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| def linear_search(values, search_for): | ||
| search_at = 0 | ||
| search_res = False | ||
|
|
||
| # Match the value with each data element | ||
| while search_at < len(values) and search_res is False: | ||
| if values[search_at] == search_for: | ||
| search_res = True | ||
| else: | ||
| search_at = search_at + 1 | ||
|
|
||
| return search_res | ||
|
|
||
| l = [64, 34, 25, 12, 22, 11, 90] | ||
| print(linear_search(l, 12)) | ||
| print(linear_search(l, 91)) | ||
|
|
||
|
|
||
| #OP | ||
| #True | ||
| #False |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| def merge_sort(unsorted_list): | ||
| if len(unsorted_list) <= 1: | ||
| return unsorted_list | ||
| # Find the middle point and devide it | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this comment seems to not be indented, could you please fix it? |
||
| middle = len(unsorted_list) // 2 | ||
| left_list = unsorted_list[:middle] | ||
| right_list = unsorted_list[middle:] | ||
|
|
||
| left_list = merge_sort(left_list) | ||
| right_list = merge_sort(right_list) | ||
| return list(merge(left_list, right_list)) | ||
|
|
||
| # Merge the sorted halves | ||
|
|
||
| def merge(left_half,right_half): | ||
|
|
||
| res = [] | ||
| while len(left_half) != 0 and len(right_half) != 0: | ||
| if left_half[0] < right_half[0]: | ||
| res.append(left_half[0]) | ||
| left_half.remove(left_half[0]) | ||
| else: | ||
| res.append(right_half[0]) | ||
| right_half.remove(right_half[0]) | ||
| if len(left_half) == 0: | ||
| res = res + right_half | ||
| else: | ||
| res = res + left_half | ||
| return res | ||
|
|
||
| unsorted_list = [64, 34, 25, 12, 22, 11, 90] | ||
|
|
||
| print(merge_sort(unsorted_list)) | ||
|
|
||
| #OP | ||
| #[11, 12, 22, 25, 34, 64, 90] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| def selection_sort(input_list): | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you do not need this extra line here |
||
| for idx in range(len(input_list)): | ||
|
|
||
| min_idx = idx | ||
| for j in range( idx +1, len(input_list)): | ||
| if input_list[min_idx] > input_list[j]: | ||
| min_idx = j | ||
| # Swap the minimum value with the compared value | ||
|
|
||
| input_list[idx], input_list[min_idx] = input_list[min_idx], input_list[idx] | ||
|
|
||
|
|
||
| l = [19,2,31,45,30,11,121,27] | ||
| selection_sort(l) | ||
| print(l) | ||
|
|
||
| #OP | ||
| #[2, 11, 19, 27, 30, 31, 45, 121] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| def shellSort(input_list): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from PEP8 this should be written in
|
||
| gap = len(input_list) // 2 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can save len(input_list) here outside and reuse it as len(input_list)) is the same across the function right? |
||
| while gap > 0: | ||
|
|
||
| for i in range(gap, len(input_list)): | ||
| temp = input_list[i] | ||
| j = i | ||
| # Sort the sub list for this gap | ||
|
|
||
| while j >= gap and input_list[j - gap] > temp: | ||
| input_list[j] = input_list[j - gap] | ||
| j = j - gap | ||
| input_list[j] = temp | ||
|
|
||
| # Reduce the gap for the next element | ||
|
|
||
| gap = gap // 2 | ||
|
|
||
|
|
||
| list = [19, 2, 31, 45, 30, 11, 121, 27] | ||
|
|
||
| shellSort(list) | ||
| print(list) | ||
|
|
||
| #op | ||
| #[2, 11, 19, 27, 30, 31, 45, 121] | ||
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.
I think this project is using the convention names in
lower_case_with_underscoresso believe you have to fix the variable names here