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

Segregate even and odd nodes in LinkedList in cpp #49

Merged
merged 3 commits into from
Oct 14, 2022
Merged
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
80 changes: 80 additions & 0 deletions CPP/Linked list/segregate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <bits/stdc++.h>
using namespace std;

class ListNode
{
public:
int val;
ListNode *next;
ListNode(int x)
{
val = x;
next = nullptr;
}
};

ListNode *head, *tail;

void PrintList(ListNode *head)
{
ListNode *curr = head;
for (; curr != nullptr; curr = curr->next)
{
cout << curr->val << "-->";
}
cout << "null" << endl;
}

void insertLast(int value)
{

ListNode *newnode = new ListNode(value);
if (head == nullptr)
{
head = newnode, tail = newnode;
}
else
{
tail = tail->next = newnode;
}
}

ListNode *segregate()
{
ListNode *oddH = new ListNode(-1), *oddT = oddH;
ListNode *evenH = new ListNode(-1), *evenT = evenH;
ListNode *curr = head, *temp;
while (curr)
{
temp = curr;
curr = curr->next;
temp->next = nullptr;

if (temp->val & 1)
{
oddT->next = temp;
oddT = temp;
}
else
{
evenT->next = temp;
evenT = temp;
}
}
evenT->next = oddH->next;
return evenH->next;
}

int main()
{
insertLast(1);
insertLast(2);
insertLast(3);
insertLast(4);
cout << "Initial : " << endl;
PrintList(head);
ListNode *newHead =segregate();
cout << "After Segregation: " << endl;
PrintList(newHead);
return 0;
}
32 changes: 32 additions & 0 deletions CPP/Sorting/insertion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include<bits/stdc++.h>

using namespace std;

int main()
{

int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
int i,j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j>0;j--)
{
if(arr[j]>=arr[j-1])
{
break;
}
swap(arr[j],arr[j-1]);
}
}
for(i=0;i<5;i++)
{
cout<<arr[i]<<" ";
}
return 0;
}
46 changes: 46 additions & 0 deletions CPP/Sorting/quick.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <bits/stdc++.h>
using namespace std;

int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++)
{
if (arr[j] < pivot)
{
i++;
swap(arr[i],arr[j]);
}
}
swap(arr[i + 1],arr[high]);
return (i + 1);
}


void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
quickSort(arr, 0, n - 1);
cout << "Sorted array: \n";
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
17 changes: 17 additions & 0 deletions Python/array/sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'''Maximum and minimum of an array.
Find Kth smallest element of the array.
Move all negative numbers to beginning and positive to end with constant extra space.
Sort the given array
Smallest Positive missing number of given array.'''


#4.Sort the given array

n=int(input("Enter size of array\n"))
arr=list(map(int,input("Enter elements of array\n").split()))
arr.sort(reverse=False) #arr.sort() also be used
print("Ascending order array")
print(*arr)
arr.sort(reverse=True)
print("Descending order array")
print(*arr)