```#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

bool isSafe(int x, int y, int n, vector<vector<int>> visited,
vector<vector<int>> &m) {
if ((x >= 0 && x < n) && (y >= 0 && y < n) && visited[x][y] == 0 &&
m[x][y] == 1) {
return true;
} else {
return false;
}
}

void solve(vector<vector<int>> &m, int n, vector<string> &ans, int x, int y,
string path, vector<vector<int>> visited) {
// you have reached x,y here

// base case
if (x == n - 1 && y == n - 1) {
ans.push_back(path);
return;
}
visited[x][y] = 1;

// 4 choices D,L,R,U
// down

int newx = x + 1;
int newy = y;
if (isSafe(newx, newy, n, visited, m)) {
path.push_back('D');
solve(m, n, ans, newx, newy, path, visited);
path.pop_back();
}

// left

newx = x;
newy = y - 1;
if (isSafe(newx, newy, n, visited, m)) {
path.push_back('L');
solve(m, n, ans, newx, newy, path, visited);
path.pop_back();
}

// right

newx = x;
newy = y + 1;
if (isSafe(newx, newy, n, visited, m)) {
path.push_back('R');
solve(m, n, ans, newx, newy, path, visited);
path.pop_back();
}

// up

newx = x - 1;
newy = y;
if (isSafe(newx, newy, n, visited, m)) {
path.push_back('U');
solve(m, n, ans, newx, newy, path, visited);
path.pop_back();
}

visited[x][y] = 0;
}

vector<string> findPath(vector<vector<int>> &m, int n) {
vector<string> ans;
if (m[0][0] == 0) {
return ans;
}
int srcx = 0;
int srcy = 0;

vector<vector<int>> visited = m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
visited[i][j] = 0;
}
}

string path = "";
solve(m, n, ans, srcx, srcy, path, visited);
sort(ans.begin(), ans.end());
return ans;
}

int main() {
int n = 4;
vector<vector<int>> m = {
{1, 0, 0, 0}, {1, 1, 0, 1}, {1, 1, 0, 0}, {0, 1, 1, 1}};
vector<string> ans = findPath(m, n);
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
cout << endl;
return 0;
}```
```void sortArray (int *arr, int n) {
if (n==0||n==1)
return;
for (int i=0; i<n-1; i++) {
if (arr[i]>arr[i+1]) {
swap(arr[i],arr[i+1]);
}
}
sortArray(arr,n-1);
}```
```void sortArray(int arr[], int n) {
for (int i = 1; i < n; i++) {
for (int j = 0; j < (n - i); j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}
```
``` const people = [
'Bernhard, Sandra', 'Bethea, Erin', 'Becker, Carl', 'Bentsen, Lloyd', 'Beckett, Samuel', 'Blake, William', 'Berger, Ric', 'Beddoes, Mick', 'Beethoven, Ludwig',
'Belloc, Hilaire', 'Begin, Menachem', 'Bellow, Saul', 'Benchley, Robert', 'Blair, Robert', 'Benenson, Peter', 'Benjamin, Walter', 'Berlin, Irving',
'Benn, Tony', 'Benson, Leana', 'Bent, Silas', 'Berle, Milton', 'Berry, Halle', 'Biko, Steve', 'Beck, Glenn', 'Bergman, Ingmar', 'Black, Elk', 'Berio, Luciano',
'Berne, Eric', 'Berra, Yogi', 'Berry, Wendell', 'Bevan, Aneurin', 'Ben-Gurion, David', 'Bevel, Ken', 'Biden, Joseph', 'Bennington, Chester', 'Bierce, Ambrose',
'Billings, Josh', 'Birrell, Augustine', 'Blair, Tony', 'Beecher, Henry', 'Biondo, Frank'
];

let sortedPeople = [];

for(person of people){
const name = person.split(',').reverse().join(' ').trim();
sortedPeople.push(name)
}

console.log(sortedPeople)```
```#include <bits/stdc++.h>
using namespace std;

void insertion_sort(int arr[] , int n)
{
for(int i=0;i<=n-1;i++){
int j = i;

while(j>0, arr[j-1] > arr[j])
{
int temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;

j--;
}
}
}
int main() {

int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}

insertion_sort(arr, n);
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}

return 0;
}```
```// c++ program to create a linked list insert element at head, at tail and delete element from
// tail, head and specific key
#include <iostream>
using namespace std;
struct Node
{
public:
int data;
Node *next;
Node(int data)
{
this->data = data;
this->next = NULL;
}
};
{
Node *NewNode = new Node(data);
}
{
Node *NewNode = new Node(data);
{
return;
}
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = NewNode;

cout << "the value of temp next is " << temp->data << endl;
}
void insertAtKey(Node *&head, int key, int data)
{
Node *NewNode = new Node(data);
{

return;
}
while (temp->data != key)
{
temp = temp->next;
if (temp == NULL)
return;
}
NewNode->next = temp->next;
temp->next = NewNode;
}
{
{
cout << head->data << " ->  ";
}
while (temp != NULL)
{
/* code */
cout << temp->data << " -> ";
temp = temp->next;
}
}

{
return;

{
delete temp;
}
}
// recursive approach to reverse the linked list
{
}
//this function reverse the k nodes in linked list
//first we have to make three node pointers

Node *prevPtr = NULL;//the prev ptr points to Null
Node *currPtr = head;//the next ptr should points toward the prev ptr because we have to reverse
//the nodes this is possible if we point the next node towards the prev node
Node *nextptr=NULL;//we will assign the value to nextptr in the loop
int count = 0;
while(currPtr!=NULL && count<k)
{
nextptr = currPtr->next;
currPtr->next = prevPtr;
prevPtr = currPtr;
currPtr = nextptr;
count++;
}
if(nextptr!=NULL){
}
return prevPtr;
}
int main()
{
cout << "insert At head" << endl;
cout << endl;
cout << "Insert at Tail " << endl;

cout << "deleting the head" << endl;
cout << endl;
cout << "reversing the linked list " << endl;
cout << endl;
cout << "reversing the k nodes in linked list " << endl;
print(NewRev);
}```
```Node*Reversek(Node*&head,int k){
//first we have to make three node pointers

Node *prevPtr = NULL;//the prev ptr points to Null
Node *currPtr = head;//the next ptr should points toward the prev ptr because we have to reverse
//the nodes this is possible if we point the next node towards the prev node
Node *nextptr=NULL;//we will assign the value to nextptr in the loop
int count = 0;
while(currPtr!=NULL && count<k)
{
nextptr = currPtr->next;
currPtr->next = prevPtr;
prevPtr = currPtr;
currPtr = nextptr;
count++;
}
if(nextptr!=NULL){
}
return prevPtr;
}```
```// c++ program to create a linked list insert element at head, at tail and delete element from
// tail, head and specific key
#include <iostream>
using namespace std;
struct Node
{
public:
int data;
Node *next;
Node(int data)
{
this->data = data;
this->next = NULL;
}
};
{
Node *NewNode = new Node(data);
}
{
Node *NewNode = new Node(data);
{
return;
}
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = NewNode;

cout << "the value of temp next is " << temp->data << endl;
}
void insertAtKey(Node *&head, int key, int data)
{
Node *NewNode = new Node(data);
{

return;
}
while (temp->data != key)
{
temp = temp->next;
if (temp == NULL)
return;
}
NewNode->next = temp->next;
temp->next = NewNode;
}
{
{
cout << head->data << " ->  ";
}
while (temp != NULL)
{
/* code */
cout << temp->data << " -> ";
temp = temp->next;
}
}

{
return;

{
delete temp;
}
}
// recursive approach to reverse the linked list
{
}
int main()
{
cout << "insert At head" << endl;
cout << endl;
cout << "Insert at Tail " << endl;

cout << "deleting the head" << endl;
cout << endl;
cout << "reversing the linked list " << endl;

}```
```Node *Reverse(Node *&head)
{
}
//in main the previous head becomes the last node
int main(){
}```
```// c++ program to create a linked list insert element at head, at tail and delete element from
// tail, head and specific key
#include <iostream>
using namespace std;
struct Node
{
public:
int data;
Node *next;
Node(int data)
{
this->data = data;
this->next = NULL;
}
};
{
Node *NewNode = new Node(data);
}
{
Node *NewNode = new Node(data);
{
return;
}
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = NewNode;

cout << "the value of temp next is " << temp->data << endl;
}
void insertAtKey(Node *&head, int key, int data)
{
Node *NewNode = new Node(data);
{

return;
}
while (temp->data != key)
{
temp = temp->next;
if (temp == NULL)
return;
}
NewNode->next = temp->next;
temp->next = NewNode;
}
{
{
cout << head->data << " ->  ";
}
while (temp != NULL)
{
/* code */
cout << temp->data << " -> ";
temp = temp->next;
}
}

{
return;

{
delete temp;
}
}
int main()
{
cout << "insert At head" << endl;
cout << endl;
cout << "Insert at Tail " << endl;

cout << "deleting the head" << endl;
}```
```#include<iostream>
using namespace std;
struct Node {
public:
int data;
Node* next;
Node(int data=0)
{
this->data = data;
this->next = NULL;
}
};
void push(Node** head_ref, int data) {
//creating the new node and by using constructor we
//are storing the value of the data
//firstly the next pointer of this new node points to NULL
//as declared in the constructor
//then we are pointing the pointer of this node to the new node
//and lastly we are equating the new insert node to the previous
//node to connect it to the list
Node* new_node = new Node(data);

}

while (temp != NULL)
{
cout << "the value of the data is " << temp->data << endl;
temp = temp->next;
}

}

int main() {
Node* node;
node = NULL;
push(&node, 5);
push(&node, 6);
push(&node, 7);
print(node);
return 0;

}```
```#include<iostream>
using namespace std;
void sort(int* array, int size) {
int key = 0, j = 0;
for (int i = 1; i < size; i++)
{
key = array[i];
j = i - 1;
while (j >= 0 && array[j] > key)
{
array[j + 1] = array[j];
j = j - 1;
}
array[j + 1] = key;
}
for (int i = 0; i < size; i++)
cout << array[i] << "  ";
}
int main() {
int size = 0;
cout << "Enter size of the array " << endl;
cin >> size;
int* array = new int[size];
cout << "Enter elements of the array " << endl;
for (int i = 0; i < size; i++)
cin >> array[i];
sort(array, size);
}```
```#include <bits/stdc++.h>
using namespace std;

int main() {
int n ;
cin>>n;
vector<pair<int , int> >p;
for(int i=0;i<n;i++){
int a, b ;
cin>>a >>b ;
int c=a+b;
p.push_back({c,a});

}
reverse(p.begin(),p.end());

int c=0;
int ans=0;
for(int i=0; i<n ;i++){
c+=p[i].first;
int sum=0;
while(i+1<n){
sum+=p[i+1].second;

i++;

}
if(c>sum){
ans=i+1;
break;
}

}

cout<< ans;
return 0;
}```
```import java.util.*;
import java.io.*;

class Solution
{
public static void main (String[] args)
{
int a[] = new int[]{10,5,30,15,7};
int l=0,r=4;

mergeSort(a,l,r);
for(int x: a)
System.out.print(x+" ");    // OUTPUT : 5 7 10 15 30

}

static void merge(int arr[], int l, int m, int h){

int n1=m-l+1, n2=h-m;
int[] left=new int[n1];
int[]right=new int[n2];

for(int i=0;i<n1;i++)
left[i]=arr[i+l];
for(int j=0;j<n2;j++)
right[j]=arr[m+1+j];

int i=0,j=0,k=l;
while(i<n1 && j<n2){
if(left[i]<=right[j])
arr[k++]=left[i++];
else
arr[k++]=right[j++];
}
while(i<n1)
arr[k++]=left[i++];
while(j<n2)
arr[k++]=right[j++];
}

static void mergeSort(int arr[],int l,int r){
if(r>l){
int m=l+(r-l)/2;
mergeSort(arr,l,m);
mergeSort(arr,m+1,r);
merge(arr,l,m,r);
}
}
}
```
```// Efficient Code :

import java.util.*;
import java.io.*;

class Solution
{
public static void main (String[] args)
{
int a[] = new int[]{10,15,20,40};
int b[] = new int[]{5,6,6,10,15};

int m = a.length;
int n = b.length;
merge(a,b,m,n);
}

static void merge(int a[], int b[], int m, int n)
{
int i=0,j=0;
while(i<m && j<n){
if(a[i]<b[j])
System.out.print(a[i++]+" ");
else
System.out.print(b[j++]+" ");
}
while(i<m)
System.out.print(a[i++]+" ");
while(j<n)
System.out.print(b[j++]+" ");
}
}

// Naive Code :

import java.util.*;
import java.io.*;

class Solution
{
public static void main (String[] args)
{
int a[] = new int[]{10,15,20,40};
int b[] = new int[]{5,6,6,10,15};

int m = a.length;
int n = b.length;
merge(a,b,m,n);

}

static void merge(int a[], int b[], int m, int n){

int[] c=new int[m+n];
for(int i=0;i<m;i++)
c[i]=a[i];
for(int j=0;j<n;j++)
c[j+m]=b[j];

Arrays.sort(c);

for(int i=0;i<m+n;i++)
System.out.print(c[i]+" ");
}
}
```
```import java.util.*;
import java.io.*;

class Solution
{
public static void main (String[] args)
{
int arr[] = new int[]{50,20,40,60,10,30};

int n = arr.length;
iSort(arr,n);

for(int x:arr)
System.out.print(x+" ");    // OUTPUT : 10 20 30 40 50 60

}

static void iSort(int arr[],int n)
{
for(int i=1;i<n;i++){
int key = arr[i];
int j=i-1;
while(j>=0 && arr[j]>key){
arr[j+1]=arr[j];
j--;
}
arr[j+1]=key;
}
}
}```
```import java.io.*;

class GFG {

static void selectionSort(int arr[], int n){
for(int i = 0; i < n; i++){
int min_ind = i;

for(int j = i + 1; j < n; j++){
if(arr[j] < arr[min_ind]){
min_ind = j;
}
}

int temp = arr[i];
arr[i] = arr[min_ind];
arr[min_ind] = temp;
}
}

public static void main (String[] args) {
int a[] = {2, 1, 4, 3};
selectionSort(a, 4);

for(int i = 0; i < 4; i++){
System.out.print(a[i] + " ");    // OUTPUT : 1 2 3 4
}
}
}```
```// Optimised Bubble Sort

import java.io.*;

class GFG {

static void bubbleSort(int arr[], int n){
boolean swapped;

for(int i = 0; i < n; i++){

swapped = false;

for(int j = 0; j < n - i - 1; j++){
if( arr[j] > arr[j + 1]){

// swapping
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;

swapped = true;

}
}
if(swapped == false)
break;
}
}

public static void main (String[] args) {
int a[] = {2, 1, 4, 3};
bubbleSort(a, 4);

for(int i = 0; i < 4; i++){
System.out.print(a[i] + " ");     // OUTPUT : 1 2 3 4
}
}
}

// Bubble Sort

import java.io.*;

class GFG {

static void bubbleSort(int arr[], int n){
for(int i = 0; i < n; i++){
for(int j = 0; j < n - i - 1; j++){
if( arr[j] > arr[j + 1]){

// swapping
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;

}
}
}
}

public static void main (String[] args) {
int a[] = {2, 1, 4, 3};
bubbleSort(a, 4);

for(int i = 0; i < 4; i++){
System.out.print(a[i] + " ");     // OUTPUT : 1 2 3 4
}
}
}```
```const useSortableData = (items, config = null) => {
const [sortConfig, setSortConfig] = React.useState(config);

const sortedItems = React.useMemo(() => {
let sortableItems = [...items];
if (sortConfig !== null) {
sortableItems.sort((a, b) => {
if (a[sortConfig.key] < b[sortConfig.key]) {
return sortConfig.direction === 'ascending' ? -1 : 1;
}
if (a[sortConfig.key] > b[sortConfig.key]) {
return sortConfig.direction === 'ascending' ? 1 : -1;
}
return 0;
});
}
return sortableItems;
}, [items, sortConfig]);

const requestSort = key => {
let direction = 'ascending';
if (sortConfig && sortConfig.key === key && sortConfig.direction === 'ascending') {
direction = 'descending';
}
setSortConfig({ key, direction });
}

return { items: sortedItems, requestSort };
}```
```# Python3 implementation of the approach

# Function to sort the array such that
# negative values do not get affected
def sortArray(a, n):

# Store all non-negative values
ans=[]
for i in range(n):
if (a[i] >= 0):
ans.append(a[i])

# Sort non-negative values
ans = sorted(ans)

j = 0
for i in range(n):

# If current element is non-negative then
# update it such that all the
# non-negative values are sorted
if (a[i] >= 0):
a[i] = ans[j]
j += 1

# Print the sorted array
for i in range(n):
print(a[i],end = " ")

# Driver code

arr = [2, -6, -3, 8, 4, 1]

n = len(arr)

sortArray(arr, n)

```
star

Sat Jul 06 2024 12:35:10 GMT+0000 (Coordinated Universal Time) https://youtu.be/GqtyVD-x_jY?list=PLDzeHZWIZsTryvtXdMr6rPh4IDexB5NIA

#c++ #dsa #recursion #sorting #string #ratinamazeproblem
star

Sun Jun 30 2024 12:20:59 GMT+0000 (Coordinated Universal Time)

#c++ #dsa #recursion #sorting #array #arraysorting #bubblesorting
star

Mon Jun 24 2024 11:09:55 GMT+0000 (Coordinated Universal Time)

#c++ #dsa #bubblesorting #sorting
star

Thu Apr 04 2024 01:24:13 GMT+0000 (Coordinated Universal Time)

#javascript #sorting
star

Mon Jul 17 2023 17:59:39 GMT+0000 (Coordinated Universal Time)

#insertionsort #array #sorting
star

Fri Aug 05 2022 11:04:40 GMT+0000 (Coordinated Universal Time)

#c++ #algorithm #dsa #sorting #insertionsort #insertion
star

Fri Aug 05 2022 11:02:38 GMT+0000 (Coordinated Universal Time)

#c++ #algorithm #dsa #sorting #insertionsort #insertion
star

Thu Aug 04 2022 11:58:14 GMT+0000 (Coordinated Universal Time)

#c++ #algorithm #dsa #sorting #insertionsort #insertion
star

Thu Aug 04 2022 11:56:51 GMT+0000 (Coordinated Universal Time)

#c++ #algorithm #dsa #sorting #insertionsort #insertion
star

Wed Aug 03 2022 17:04:50 GMT+0000 (Coordinated Universal Time)

#c++ #algorithm #dsa #sorting #insertionsort #insertion
star

Sun Jul 24 2022 15:28:39 GMT+0000 (Coordinated Universal Time)

#c++ #algorithm #dsa #sorting #insertionsort #insertion
star

Thu Jul 21 2022 12:16:00 GMT+0000 (Coordinated Universal Time)

#c++ #algorithm #dsa #sorting #insertionsort #insertion
star

Mon Jun 20 2022 10:05:00 GMT+0000 (Coordinated Universal Time) https://www.codechef.com/ide?itm_medium

#disctionary #sorting
star

Tue Feb 08 2022 15:08:17 GMT+0000 (Coordinated Universal Time)

#java #gfg #geeksforgeeks #lecture #sorting #mergesort
star

Tue Feb 08 2022 15:00:15 GMT+0000 (Coordinated Universal Time)

#java #gfg #geeksforgeeks #lecture #sorting #merge #sortedarrays
star

Tue Feb 08 2022 14:56:44 GMT+0000 (Coordinated Universal Time)

#java #gfg #geeksforgeeks #lecture #sorting #insertionsort
star

Tue Feb 08 2022 14:54:48 GMT+0000 (Coordinated Universal Time)

#java #gfg #geeksforgeeks #lecture #sorting #selectionsort
star

Tue Feb 08 2022 14:45:51 GMT+0000 (Coordinated Universal Time)

#java #gfg #geeksforgeeks #lecture #sorting #bubblesort
star

Fri Dec 24 2021 18:05:03 GMT+0000 (Coordinated Universal Time) https://www.smashingmagazine.com/2020/03/sortable-tables-react/

#react.js #sorting
star

Thu Dec 26 2019 15:35:22 GMT+0000 (Coordinated Universal Time) https://www.geeksforgeeks.org/sort-an-array-without-changing-position-of-negative-numbers/

#python #interesting #arrays #sorting #interviewquestions