Skip to content

Commit c874107

Browse files
authored
Merge pull request #229 from Sheetal777/master
Added Array searching, sorting, and Data Structure scripts and included badges in Readme.md
2 parents e60437e + 2139485 commit c874107

File tree

4 files changed

+194
-1
lines changed

4 files changed

+194
-1
lines changed

Arrays-Sorting/src/oddEvenSort.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
#Python Program to implement Odd-Even / Brick Sort
3+
#A python program to implement odd-even sorting or Brick Sort.
4+
#It is a kind of bubble sort since it is divided into two phases,
5+
#i.e. odd phase & even phase and bubble sort is implemented on each of the phases.
6+
def oddEvenSort(arr, n):
7+
isSorted = 0
8+
while isSorted == 0:
9+
isSorted = 1
10+
temp = 0
11+
for i in range(1, n-1, 2):
12+
if arr[i] > arr[i+1]:
13+
arr[i], arr[i+1] = arr[i+1], arr[i]
14+
isSorted = 0
15+
16+
for i in range(0, n-1, 2):
17+
if arr[i] > arr[i+1]:
18+
arr[i], arr[i+1] = arr[i+1], arr[i]
19+
isSorted = 0
20+
21+
return
22+
23+
arr = [34, 2, 10, -9]
24+
n = len(arr)
25+
oddEvenSort(arr, n);
26+
27+
for i in range(0, n):
28+
print(arr[i], end = ' ')
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Recursive function to search x in arr[l..r]
2+
# Another python program to search an element linearly in an array using Recursion (Read again 💯 )
3+
#Why recursion? Because it makes the task easier and reduces time complexity.
4+
def rec_search( arr, l, r, x):
5+
if r < l:
6+
return -1
7+
if arr[l] == x:
8+
return l
9+
if arr[r] == x:
10+
return r
11+
return rec_search(arr, l+1, r-1, x)
12+
13+
# Main Code
14+
arr = [12, 34, 54, 2, 3]
15+
n = len(arr)
16+
x = 3
17+
index = rec_search(arr, 0, n-1, x)
18+
if index != -1:
19+
print "Element", x,"is present at index %d" %(index)
20+
else:
21+
print "Element %d is not present" %(x)
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#Another python program to implement and Insert a node by taking an pointer pointing to locations one want to insert a node to.
2+
#The insertion operations are carried out:
3+
#a. in empty linked list
4+
#b. at beginning of the linked list
5+
#c. at end of the linked list
6+
#d. in between the nodes
7+
8+
class Node:
9+
10+
def init(self, data):
11+
12+
self.data = data
13+
14+
self.next = None
15+
16+
class CircularLinkedList:
17+
18+
def init(self):
19+
20+
self.last = None
21+
22+
# for empty list
23+
24+
def addToEmpty(self, data):
25+
26+
if (self.last != None):
27+
28+
return self.last
29+
30+
# Creating the newnode temp
31+
32+
temp = Node(data)
33+
34+
self.last = temp
35+
36+
# Create Link
37+
38+
self.last.next = self.last
39+
40+
return self.last
41+
42+
def addBegin(self, data):
43+
44+
if (self.last == None):
45+
46+
return self.addToEmpty(data)
47+
48+
temp = Node(data)
49+
50+
temp.next = self.last.next
51+
52+
self.last.next = temp
53+
54+
return self.last
55+
56+
def addEnd(self, data):
57+
58+
if (self.last == None):
59+
60+
return self.addToEmpty(data)
61+
62+
temp = Node(data)
63+
64+
temp.next = self.last.next
65+
66+
self.last.next = temp
67+
68+
self.last = temp
69+
70+
return self.last
71+
72+
def addAfter(self, data, item):
73+
74+
if (self.last == None):
75+
76+
return None
77+
78+
temp = Node(data)
79+
80+
p = self.last.next
81+
82+
while p:
83+
84+
if (p.data == item):
85+
86+
temp.next = p.next
87+
88+
p.next = temp
89+
90+
if (p == self.last):
91+
92+
self.last = temp
93+
94+
return self.last
95+
96+
else:
97+
98+
return self.last
99+
100+
p = p.next
101+
102+
if (p == self.last.next):
103+
104+
print(item, "not present in the list")
105+
106+
break
107+
108+
def traverse(self):
109+
110+
if (self.last == None):
111+
112+
print("List is empty")
113+
114+
return
115+
116+
temp = self.last.next
117+
118+
while temp:
119+
120+
print(temp.data, end = " ")
121+
122+
temp = temp.next
123+
124+
if temp == self.last.next:
125+
126+
break
127+
128+
if name == 'main':
129+
130+
llist = CircularLinkedList()
131+
132+
last = llist.addToEmpty(6)
133+
134+
last = llist.addBegin(4)
135+
136+
last = llist.addBegin(2)
137+
138+
last = llist.addEnd(8)
139+
140+
last = llist.addEnd(12)
141+
142+
last = llist.addAfter(10,8)
143+
144+
llist.traverse()

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# **Scripts Dump!**
2+
[![Open Source Love](https://firstcontributions.github.io/open-source-badges/badges/open-source-v1/open-source.svg)](https://github.com/firstcontributions/open-source-badges) [![Pull Requests Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat)](http://makeapullrequest.com)
23

34
> Your goto place to find and dump any script you want.
45
>
@@ -45,7 +46,6 @@ ScriptsDump is a complete repository of all kind of scripts we and you can think
4546
- **[Graph Algorithms](/Graph_Algorithms/src)**
4647

4748

48-
4949
## Maintainers
5050

5151
We're always looking out for people who're enthusiastic to work and collaborate with people. If you want to become a maintainer at ScriptsDump you can contact us [email protected]

0 commit comments

Comments
 (0)